i've written a nonrecursive fibonacci function and my purpose is to finding the largest fibonacci number that can be printed by my system.
the below code is what i've written,but i don't know what i should write in the while loop,to find the largest number.
cout<<"n : "<<n<<" , "<<"fib : "<<fib(n)<<endl;
int fib(int x)
f = f1 + f2;
f1 = f2;
f2 = f;
When there is an integer overflow, the bits overflow into the sign bit of the integer and it becomes negative. As you fib() function is returning an integer, it will return a negative value if there is an integer overflow ie. if the number is too big to be represented in an int as a positive value. So, all you have to do is keep generating fibonacci numbers till you get a negative number.
int n = 2 ;
while( fib(n) >= 0 )
cout << "n : " << n << " , " << "fib : " << fib(n) << '\n' ;