I'm learning about tail recursion, and before I hand in this problem, I'd like a yes / no answer to whether the code snippet is tail recursive or not.
int fib_in(int n, int current, int prev) {
if (n == 1 || n == 2) { // n = 1 or 2 both gives fib number 1
return current;
}
return fib_in(n - 1, current + prev, current); // recursive call, current gets updated and new prev is the current, so were going backwards if that makes sense
}
int fib(int n) {
return fib_in(n, 1, 1); // 1 and 1 is the 2 first fib numbers so theyll be used as base cases
}
int main(void) {
int n, f;
printf("the nth number: ");
scanf("%d", &n);
// call fib function
f = fib(n);
printf("%d \n", f);
return 0;
}
I definitely think it is, but I made this function in response to another homework assignment, before we learned about tail recursion, so I would've used a technique I didn't know existed. Which is why I'm kinda confused.