Can you write this in any simpler?

The program generates Fibonacci numbers until the number reaches the limit (maks) that was entered by the user.It works perfectly fine but I think that this code could be written in a more efficient and cleaner way.What do you think? Is this okay?

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
#include <iostream>
using namespace std;
int first=0,second=1,maks,number=1;
int main()
{
cout<<"Enter limit";
cin>>maks;
cout<<first;
cout<<second;

while(number<maks)
{
 number=(first+second);
 first=second;
 second=number;

 if (number<maks)
 {
 cout<<number;
 }
}
 system("pause");
return(0);
}
Consider a do ... while loop. It always executes at least once and may spare you the extra if.
http://cplusplus.com/doc/tutorial/control/
Last edited on
Topic archived. No new replies allowed.