Oct 5, 2013

Program: Display Fibonacci Series IN C++

Program : DISPLAY FIBONACCI SERIES IN C++

Check out Program

#include<iostream.h>
#include<conio.h>
// Display Fibonacci Series 
// In Fibonacci Series first two numbers in the Fibonacci series are 0 and 1, and each subsequent number is the sum of the previous two.
int main()
{
int a=0,b=1, c,n; 
cout << "Enter the value of n ";
cin >> n; // n store the integer value that series will shown upto n terms
cout << endl << a <<" " <<b << " " ;
for(int i =0; i<(n-2); i++)
{
c = a+ b;
cout << c << " ";
a=b;
b=c;
}
/* First we Print first Two Term of Series then using for loop we generate rest of series upto n term. Let Begin with loop first it begin from 0 till the n-2 term because we already print first two term of series, then inside loop we initialize c = a + b, c contain value sum of a + b which become the third term of series. In Fibonacci series sum of previous two term is the next term. After getting value of next term and store in variable c. Then swapping of variable take place, now we don't require first term for next term(i.e. 4th term) so we replace value of a with b and replace value of b with c. Now 1st contain 2nd and 2nd contain 3rd, so they can generate 4th and so on. This process going on until the nth term of series. It will Print output after every assigning value of c.
*/
getch(); //http://cplspls.blogspot.com
return 0; 
}

OUTPUT:




Above Program Without Comments
#include<iostream.h>
#include<conio.h>
int main()
{
int a=0,b=1, c,n; 
cout << "Enter the value of n ";
cin >> n; 
cout << endl << a <<" " <<b << " " ;
for(int i =0; i<(n-2); i++)
{
c = a+ b;
cout << c << " ";
a=b;
b=c;
}
getch(); //http://cplspls.blogspot.com
return 0; 
}

10 comments:

  1. thanks bos....cuma disini yg bener bener pake Cpp ,di tempat lain cuma judulnya doang menggunakan Cpp tapi waktu di lihat malah Pakai VS -_-",dunia penuh dengan tipu tipu.

    ReplyDelete
  2. thanks for good explanation

    ReplyDelete
  3. New Fibonacci Algo discovered:

    function FiboMaxE(n: byte): Extended;
    begin
    result:= (pow((1+sqrt(5))/2,n)-pow((1-sqrt(5))/2,n))/sqrt(5)
    end;

    ReplyDelete
  4. Very informative article.Thank you author for posting this kind of article .

    http://www.wikitechy.com/view-article/Fibonacci-series-program-in-cpp


    Both are really good,
    Cheers,
    Venkat

    ReplyDelete
  5. your output is wrong, answer if your enter a number 10, the answer is 55.

    ReplyDelete
  6. This program print upto nth term so 10 means 10 digits will be print. So its correct

    ReplyDelete