Hi
Which version of the two do you prefer? I prefer the "1" because the while condition seems more understandable to me (don't really know if it's correct or not). Any suggestions.
EDIT: There is some problem with CODE 1. It just keeps running.
And I'm also getting this warning: warning: this decimal constant is unsigned only in ISO C90|
What does this warning mean? Could you please tell me? Thanks.
CODE 1
Code:
// fibo.cpp
// demonstrates WHILE loops using fibonacci series
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
const unsigned long limit = 4294967295; //largest unsigned long
unsigned long next_to_last=0; //next-to-last term
unsigned long last=1; //last term
while( (next_to_last + last) < limit ) //don't let results get too big
{
cout << last << " "; //display last term
long new_last = next_to_last + last; //add last two terms
next_to_last = last; //variables move forward
last = new_last; // in the series
}
cout << endl << endl;
system("pause");
return 0;
}
CODE 2
Code:
// fibo.cpp
// demonstrates WHILE loops using fibonacci series
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
const unsigned long limit = 4294967295; //largest unsigned long
unsigned long next_to_last=0; //next-to-last term
unsigned long last=1; //last term
while( next_to_last < limit / 2 ) //don't let results get too big
{
cout << last << " "; //display last term
long new_last = next_to_last + last; //add last two terms
next_to_last = last; //variables move forward
last = new_last; // in the series
}
cout << endl << endl;
system("pause");
return 0;
}