mirror of
https://github.com/sigmasternchen/fibonacci
synced 2025-03-15 08:08:58 +00:00
add c version
This commit is contained in:
parent
124e4d81a2
commit
9cdc05b70e
1 changed files with 26 additions and 0 deletions
26
c/fibonacci.c
Normal file
26
c/fibonacci.c
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in a new issue