19 March, 2012

C++ Program For Printing Fibonacci Series.

This Post Contains A C++ Program For Printing Fibonacci Series With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'While-loop' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple
& Easy-To-Understand Way.


What Is A Fibonacci Series ?.

The Fibonacci Sequence Is The Series Of Numbers :

0,   1,   1,   2,   3,   5,   8,   13,   21,   34,   .....

The next number is found by adding up the two numbers before it.
  • The 2 is found by adding the two numbers before it (1+1)
  • Similarly, the 3 is found by adding the two numbers before it (1+2),
  • And the 5 is (2+3), & so on.
Following Is The Simple & Easy To Understand Algorithm Of Fibonacci Series, Which Will Help You To Analyse The Source Code Of This Program.

Algorithm :

1.  Assign sum=0,  A=0,  B=1,  i=1.
2.  Get the no. of terms upto which u want to generate the Fibonacci no,  i.e.,  n.
3.  Add A and B to get the next Fibonacci number. 4. Assign the value of B to A i.e. A=B
5.  Assign the value of sum to B  i.e.  B = sum.
6.  Write the value of su to get next Fibonacci number in the series.
7.  Increment  i  with  1  i.e.  i = i + 1 and repeat step 3, 4, 5, 6 with the last value of i = n ( n is the no. of terms which u  want to generate Fibonacci no. series. )
8. Stop.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program For Printing A Fibonacci Series.

# include <iostream.h>
# include <conio.h>

void main()
{
   int a, b, c, value;
   clrscr();
   cout << " Enter Any Value : ";
   cin >> value;
   a  =  0;  b  =  1;  c  =  0;
   cout << a << "\t" << b;
   c  =  a  +  b;
   while ( c   <=   value )
   {
      cout << "\t" << c;
      a  =  b;
      b  =  c;
      c  =  a  +  b;
   }
   getch();
}

Output :

Enter Any Value  :  55

0   1   1   2    3    5     8   13    21     34     55




No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.