From 9cdc05b70e779ead8502dd3d53c7cf8cb5b123b3 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Wed, 8 Jul 2020 15:40:30 +0200 Subject: [PATCH] add c version --- c/fibonacci.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 c/fibonacci.c diff --git a/c/fibonacci.c b/c/fibonacci.c new file mode 100644 index 0000000..1296cef --- /dev/null +++ b/c/fibonacci.c @@ -0,0 +1,26 @@ +#include +#include + + int fibonacci(int a, int b, int limit, void (*forEach)(int, int, int)) { + if (limit == 0) + return b; + + int c = a + b; + forEach(a, b, c); + return fibonacci(b, c, limit - 1, forEach); +} + +void printIteration(int a, int b, int c) { + printf("%d + %d = %d\n", a, b, c); +} + +int main(int argc, char** argv) { + int limit = 3; + if (argc > 1) + limit = atoi(argv[1]); + + int result = fibonacci(1, 1, limit, printIteration); + printf("Result: %d\n", result); + + return 0; +}