You are here

C++ program to generate Fibonacci series

C++ program for Fibonacci series.

C++ programming code

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. main()
  6. {
  7.    int n, c, first = 0, second = 1, next;
  8.  
  9.    cout << "Enter the number of terms of Fibonacci series you want" << endl;
  10.    cin >> n;
  11.  
  12.    cout << "First " << n << " terms of Fibonacci series are :- " << endl;
  13.  
  14.    for ( c = 0 ; c < n ; c++ )
  15.    {
  16.       if ( c <= 1 )
  17.          next = c;
  18.       else
  19.       {
  20.          next = first + second;
  21.          first = second;
  22.          second = next;
  23.       }
  24.       cout << next << endl;
  25.    }
  26.  
  27.    return 0;
  28. }