Wednesday, 20 November 2013

C++ : Fibonacci Series


The below code finds the Fibonacci series for the entered number of terms :  

#include<iostream>
using namespace std;
main()
{
   int n, c, a = 0, b = 1, temp;
   cout << "Enter the number of terms" << endl;
   cin >> n;
   cout << "The Fibonacci series are :- " << endl;
   for ( c = 1 ; c < n ; c++ )
   {
      if ( c <= 1 )
         temp = c;
      else
      {
         temp = a + b;
         a = b;
         b = temp;
      }
      cout << temp << endl;
   }
   return 0;
}

Input :

Enter the number of terms : 7

Output :

f = 
f = 
1 1 
f =
1 1 2 
f =
1 1 2 3 
f =
1 1 2 3 5 
f =
1 1 2 3 5 8
f =
1 1 2 3 5 8 13

To know about the Fibonacci Series in detail and also to know the codes of the Fibonacci Series in various other programming languages  Click Here

If the above link doesn't work go to this URL :
http://gappcode.blogspot.in/2013/11/fibonacci-series.html

No comments:

Post a Comment