Fibonacci Sequence

I am trying to write a program that asks the user for a specific number then displays that many Fibonacci numbers using a for loop.

All I know is basic c++, I dont know how to use return functions or anything like that. Just basic arithmetic +,-,*,/,%. I can also use else if but a for loop must be used.

Can please someone help.
Algorith(may be slightly incorrect because I haven't tested it)

1)Input number
2)first=0,second=1
3)if number=1,cout first
4)else cout first,second for x=0;x<n-2;x++ fibno=first+second first=second second=fibno cout fibno

I will not post code here.
I remember I wrote here such a program not so far. Try to search it in this forum.:)

Well, I have found it myself. You can use it as a base code.


1
2
3
4
5
6
7
8
9
10
11
12
unsigned fibonacci[] = { 0, 1 };

std::cout << "Fibonacci seria less than or equal to 100: "; 

while ( ( fibonacci[1] += fibonacci[0] ) <= 100 ) 
{
	std::cout << ' ' << fibonacci[1];

	fibonacci[0] = fibonacci[1] - fibonacci[0];
}

std::cout << std::endl;


I think it would be even more correctly to define the array the following way

unsigned fibonacci[] = { 1, 0 };
Last edited on
try to create it yourself.
Topic archived. No new replies allowed.