We have our final tomorrow and my teacher said we dont have to right the int fun
ID: 3695490 • Letter: W
Question
We have our final tomorrow and my teacher said we dont have to right the int functions or int main i forget but its one of them. Doesnt that mean we don't have to write the entire code but mostly some of it? Also am I suppose to write the cout<<6th Fibonacci number is"<<Fiboncacci(6)<<endl;
Question 8. Write a recursive function called Fibonacci(n) that returns nth number in Fibonacci series. In fibonnaci series each number after the first number is the summation of last two numbers. For example following sequence is Fibonacci series:
1,1,2,3,5,8,13,21,34... That means Fibonacci(6) should be 8 because the 6th Fibonacci number is 8 in that series [5]
[hint: You must have a termination condition and also, express the Fibonacci number in recursive equation form based on the above definition]
int Fibonacci(int n){
if(n==1 || n==2)
return 1;
else
return Fibonacci(n-1)+Fibonacci(n-2);
}
Explanation / Answer
Yes. you have to write only function to calculate n th fibonacci number.
The recursive calls are as follows:
Fibonacci(6)=Fibonacci(5)+Fibonacci(4)
=(Fibonacci(4)+Fibonacci(3)) + ( fibonacci(3)+Fibonacci(2)
=Fibonacci(3)+Fibonacci(2) + fibonacci(2)+Fibonacci(1) + (fibonacci(2)+Fibonacci(1) +1
=(fib(2)+fib(1))+ 1+ 1+ 1+ 1+1+1
=1+1+1+ 1+ 1+ 1+1+1
=8
Recursive equation
Fn= F n-1+F n-2 for n>2
else Fn=1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.