Demonstrate WHILE loops using fibonacci series - C++ Statement

C++ examples for Statement:while

Description

Demonstrate WHILE loops using fibonacci series

Demo Code

#include <iostream>
using namespace std;
int main()//from  w w w. j a  va  2 s  .c  o  m
{                           //largest unsigned long
    const unsigned long limit = 4294967295;
    unsigned long next=0;       //next-to-last term
    unsigned long last=1;       //last term
    while( next < limit / 2 )   //don't let results get too big
    {
       cout << last << "  ";    //display last term
       long sum = next + last;  //add last two terms
       next = last;             //variables move forward
       last = sum;              //   in the series
    }
    cout << endl;
    return 0;
}

Result


Related Tutorials