Fibonacci Numbers

Hi you guys I hope you are having a great day so far. I have a question. I am supposed to write a program that starts with the number 1 and adds that number to the last number. My example shows me starting at 0 but not counting it, then the next number is 1 and then the next is one. those two numbers are added together and then the sum of those are added back to the last number. For example the numbers would be 0, 1, 1, then 1+1 is 2 then the 2 is added to the 1 and then I have 3 then the 3 and 2 are added together and then I get 5 and so on. I have started but the program does nothing. This is what I have so far.


#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;


int _tmain( )
{
int previous1; // variable for the first number
int previous2; // variable for the second number
int current; // varible to store the current number
int counter; //loop control varible

cout << "Fibonacci Series #1 = 0" << endl;
cout << "Fibonacci series #2 = 1" << endl;
cin >> previous1;
cout << "Fibonacci series #3 = 1" << endl;
cin >> previous2;



for( previous1 = 1; previous2 = 1; counter++ )
{
current = previous2 + previous1;
previous1 = previous2;
previous2 = current;
counter++;
}







system ("pause");
return 0;
}
Can you help me see what I am doing wrong please. p.s. I am supposed to start counting my series at 1-25

Last edited on
I guess normally a program should produce some kind of output. Maybe add a cout or two?
I do not understand what do previous1 and previous2 mean? Why did you enter them at first and then rewrite them in the loop?
I'm trying to get the series to count from 0 to 25 starting at 1. The numbers I need are 0, 1, 1, 2, 3, 5, 8, 13, and so on. I thought the loop would continue to count for me. Am I not supposed to do this?
I am sorry but I have understtood nothing. What are these statements for?

cout << "Fibonacci series #2 = 1" << endl;
cin >> previous1;
cout << "Fibonacci series #3 = 1" << endl;
cin >> previous2;

When the program runs its supposed to have the following:
The first 25 numbers in the Fibonacci series:
Fibonacci Series #1 = 0
Fibonacci Series #2 = 1
Fibonacci Series #3 = 1
Fibonacci Series #4 = 2
Fibonacci Series #5 = 3

and the program is supposed to keep counting the numbers added together until it gets to Fibonacci Series # 25 = 46368.
I am trying to loop it to get the program to keep running.
Last edited on
One more what are these statements for?

cout << "Fibonacci series #2 = 1" << endl;
cin >> previous1;
cout << "Fibonacci series #3 = 1" << endl;
cin >> previous2;


What are you trying to enter in these two varaibles?
I thought I needed to declare what the first two numbers were. Do I not need them there?
1
2
3
4
5
6
7
for( previous1 = 1; previous2 = 1; counter++ )
{
current = previous2 + previous1;
previous1 = previous2;
previous2 = current;
counter++;
}

that for statement is invalid.The second thingy (can't think of the name) in a for statement is called every time the for code comes to an end (repeat if it returns true and don't repeat if it returns false). Since previous2 = 1 is your second thingy, previous2 will be set equal to 1 every time the for code is ran.

1
2
3
4
5
6
7
for( previous1 = 1; previous2 = 1; counter++ )
{ //even though it was previously set equal to 2, previous2 will be reset equal to 1 before the code below is ran
current = previous2 + previous1; //1+1
previous1 = previous2; //1
previous2 = current; //2
counter++;
}


Try this:

1
2
3
4
5
6
7
8
9
10
previous1 = 1;
previous2 = 1;

for (counter = 4;counter < 26;counter++)
{
current = previous2 + previous1;
cout<<"Fibonacci series #<<counter<< = <<current<<" << endl;
previous1 = previous2;
previous2 = current;
}


Also, cin >> previous1 and cin >> previous have no use. Get rid of them. Third, counter isn't set equal to anything so that needs to be fixed(see above). Lastly, you might want to add a cout (see above). Try my suggestions and tell me if they work (I didn't test them out before replying)

That helped thank you . I changed a couple of things but now it all works correctly but it comes out with series #2 all the way down.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 #include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
	



int _tmain( )
{
	int previous;
	int previous1; // variable for the first number
	int previous2; // variable for the second number
	int current; // varible to store the current number
	int counter; //loop control varible

previous=0;
previous1 = 1;
previous2 = 1;
cout << "The first 25 numbers in the Fibonacci Series:" << endl;
cout << "Fibonacci Series #1 = " << previous << endl;

for (counter = 1;counter <= 25;counter++)
{
current = previous2 + previous1;
cout<<"Fibonacci series #2=" <<previous + previous1 << endl;
previous1 = previous2;
previous2 = current;
}
		
	system ("pause");
	return 0;
}
First, Your welcome.

Second,
cout<<"Fibonacci series #2=" <<previous + previous1 << endl;
It comes out with #2 because your telling it to. Just replace the 2 with <<counter + 1<< and then try it.

cout<<"Fibonacci series #"<<counter+1<<"=" <<previous + previous1 << endl;
Last edited on
Niven, I figured it out last night but, again I wanted to thank you for helping me. I hope someday I will be as good as you guys are. I guess some people just pick this stuff up but I am only on my 6th or 7th week. I have to tell you that I am taking networking too and I really don't get it. But anyway have a great day. By the way how long have you been at this?
I've been at it for some months now (maybe like 6 or 7) and am not really all too good. What I know I've learned purely from internet tutorials and googling specific topics so my education in programming isn't what it should've been. I just know the basics (anything I make that's even a little bit complicated is made through me trying to come up with creative solutions using what I know) of regular c++ and some, but even less, of opengl (a graphics api), but all that's not important. Anyway, good luck with programming and I hope you get better than me someday.
I don't recall Fibonacci numbers ever starting with 0. Sounds like 0+1 always kept you in a bad loop - at first.
Topic archived. No new replies allowed.