i will try to explain this as best as i can. let say that the user input 100. i want it to stop when the fibonacci numbers added up to 100. not the 100th time.
how do i make it happen? i can't seem for figure out
(im a beginner at c++ so please take it easy on me)
#include<iostream>
usingnamespace std;
int main()
{
int range, first = 0, second = 1, fibonicci=0;
cout << "Enter the upper limit for fibonacci number: ";
cin >> range;
for ( int c = 0 ; c < range ; c++ )
{
if ( c <= 1 )
fibonicci = c;
else
{
fibonicci = first + second;
first = second;
second = fibonicci;
}
cout << fibonicci;
}
return 0;
}
I'm not entirely sure what you mean. Do you mean to show the fibonacci number just before the limit, or when adding each and every fibonacci number is just less than the limit?
Assuming the former, change the condition of the loop from a for loop to something like while (fibonacci < range), and then subtract first from it to get the value before in the series (i.e. the one just before it went out of range).
That is still a loop. You want to continue as long as the latest computed Fibonacci number remains below the range. You should consider the dowhile loop.
Hint: Do not compute a number and then show it. Show the number and then compute next.