The problem is this. I try to enter the limit of at least 34 fibonacci numbers but the limit display the limit of 89.

#include <iostream>
using namespace std;
int main()
{
    
    int x=0,y=1;
    int num;
    
    cout<<"Enter number:";
    cin>>num;
    cout<<x<<" "<<y;
    
    while(y<=num)
    {
          x= x + y;
          y= y + x;
      
           cout<<" "<<x<<" "<<y;
    }
    
    system("pause");
    
return 0;
}

Your question is confusing, but the "limit" is based on the value of y, not the count of fibonacci numbers you've printed. Perhaps you wanted the latter?

It is because of the while(y<=num) statement. When your iteration, where x = 21 and y = 34, is complete: it checks if y is <= num (which it is), so it continues executing. If you changed it to while(y<num) it would stop at 34.

while(y<=num)
    {
          x= x + y;
          y= y + x;
 
           cout<<" "<<x<<" "<<y;
    }

let's say you don't want y to be bigger than 34. Let's say num=34 and y=33. Look at the above code. It sees that y<=num, and runs what is inside the brackets. Inside the brackets, y increases above the number you want, then prints.

perhaps you want something along the lines of

do
    {
          x= x + y;
          y= y + x;
 
           cout<<" "<<x<<" "<<y;
    } while(y<=num)

woah I get it I so I added the if conditional value before it prints to y.thanks it means a lot. it was my first program using my own logic,since I'm a newbie ^.^

do
{
    x= x + y;
    
    y= y + x;
 
    cout<<" "<<x<<" ";
    if(y<=num)
    cout<<y;
} 
while(num>y);
int main()
{
  int num =1;
  int a = 0, b = 1;
  int count = 1;

  while (count <= 15-num )
  {
    b = a + b;
    a = b - a;
    b = a + b - a;
    cout<< b<< " ";
    count++;
  }
  cout<<endl;
  return 0;
}

Edited 5 Years Ago by Narue: added formatting and code tags

This article has been dead for over six months. Start a new discussion instead.