Mar
In the Fibonacci series the first two terms are 0 and 1 and every subsequent term is the sum of the two preceding terms. So the first 7 element of the series are as follows:
F0=0
F1=1
F2=1 (F0 F1)
F3=2 (F1 F2)
F4=3 (F2 F3)
F5=5 (F3 F4)
F6=8 (F4 F5)
F7=13 (F5 F6)
Write a recursive function fib(n) that returns the nth Fibonacci number. DO NOT use any loops in the function. The program should read n from the user and then call the fib function to compute the value.
When I write the program it is an integer off. It shows the F1=0 instead of 1. Please help me!!
Answer:
#include
void fib(int n)
{
int i=0,s[20];
abc: if(i<=n){if(i==0)s[i]=i;
else if(i==1)s[i]=1;
else s[i]=s[i-2] s[i-1];
cout<
i ;
}
goto abc;
}
void main()
{
int n;
cout<<"\n Enter the number of terms ";
cin>>n;
fib(n);
}
sry, am writin dis in a hurry hope dis works
Answer:
Yeah, if you wrote it then show it.
Answer:
f(1) (like f(0)) should be an explicit special case in your code and not rely on any of the recursion. Book Mark it-> del.icio.us | Reddit | Slashdot | Digg | Facebook | Technorati | Google | StumbleUpon | Window Live | Tailrank | Furl | Netscape | Yahoo | BlinkList