int main()
{
int a,b,c,d;
b=0;
c=1;
d=1;
cout << “Which Fib do you want to find?”;
cin >> a;
do
{
cout << c << endl;
c = c + d;
cout << d << endl;
d = c + d;
b++;
}
while (b<a)
cout << “And those are the fibs” << endl;
}
I’m not sure what this segment is supposed to do; a quick look at the source didn’t reveal much (maybe I’m just being dense, but I didn’t get it), but it won’t even compile without a semicolon ( at the end of the while.
Here’s the output from the test run I gave it:
Which Fib do you want to find?5
1
1
2
3
5
8
13
21
34
55
And those are the fibs
Good luck with this and post back if you need help,
Just run a test before the number is outputted. If the number is < the inputted number, continue, if number == the inputted number, end with result, if number > inputted number, return error to user.
int fib(int number)
{
if(number==0 || number ==1) return number;
else return fib(number-1)+fib(number-2);
}
yay.
User enters a term, it spits out the fib value for that term.
Look at the inductive definition of Fibonacci series if you want to see why this works
It runs in N*N time though.There are alot of clever optimizations that can be done to make this faster.