Trying to calculate the Fibonacci numbers using a recursive function, but my code is using 2 recursive calls. Is it possible to do it using just one? Would saving the fib number of n-1 into an array or similar and then adding the numbers at end of function, would that work?
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int fib(int n) {
assert(n >= 0);
if (n > 1)
return fib(n - 1) + fib(n - 2);
else if (n == 1)
return 1;
else
return 0;
}
int main(void) {
int n, f;
printf("the nth number: ");
scanf("%d", &n);
f = fib(n);
printf("%d \n", f);
return 0;
}