fibonacci

Mar 5, 2014 at 9:50pm
i know something is wrong with it help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main () 
{
    int n=1, f=1, sum;
	
	for (int n=0; n<20; n++) 
	{
		cout << f << " ";
		sum=n+f;
		n=f;
		f=sum;
	}
    return 0;
}

Mar 5, 2014 at 9:59pm
Your loop index is n. That messes up your n.
Mar 5, 2014 at 10:11pm
try this:

#include <iostream>

using namespace std;

int main ()
{
int f=1,e, s = 0;

while ( f<20)
{
e = s;
s = f;
cout << f << " ";
f = e + s;
}
return 0;

}

i had also a fibonacci code and i put that, sory about the different variable names and i prefer directly put this code than saying that where you mistake in your code, also sorry for this.
Last edited on Mar 5, 2014 at 10:13pm
Mar 5, 2014 at 10:53pm
Thanks that dose work
Topic archived. No new replies allowed.