That was a bad title, but hopefully I can clear it up here.
Ok, so the assignment is to use a recursive function to display a request for a certain position in the Fibonacci Sequence (0, 1, 1, 2, 3, 5, 8, 13...)
The program I have written works, but it displays the wrong number in sequence. The book says that when you enter "8" you should receive "13", which is correct in the order of the sequence, though my program is producing "21" which is the next number in the sequence.
I tested it with a few more numbers and when I enter "7" it produces "13" and when I enter "6" it produces "8" and so on.
Can anyone help me out with why it is calculating one above instead of the right number?
Code:
//Cameron Taylor
#include <stdio.h>
int main(){
int n, result;
int Fib(int);
printf("Enter the number in sequence you are looking for: ");
scanf("%d", &n);
result = Fib(n);
printf("\n The position in the sequences result is %d.\n", result);
return 0;
}
int Fib(int n){
if (n < 2)
return (n);
else
return (Fib(n-1) + Fib(n-2));
}
thanks!