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;
}
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;
}