Wednesday, 27 August 2014

Filled Under: ,

C++ program to generate Fibonacci series

Share
Fibonacci series is a sequence of numbers in which every number is equal to the sum of  previous two numbers.Here a program is given below which take the first two number and the number of terms as input  and print a series on output screen.
C++ program to generate Fibonacci series

C++ program to generate Fibonacci series

#include<iostream.h>
int main()
{
int first, second, sum, num, counter = 0;
cout<<"\nEnter the term : ";
cin>>num;
cout<<"\nEnter First Number : ";
cin>>first;
cout<<"\nEnter Second Number : ";
cin>>second;
cout<<"\nFibonacci Series :"<<first<<" "<<second;
while (counter < num)
{
sum = first + second;
cout<<sum;
first = second;
second = sum;
counter++;
}
return (0);
}


 Output:
Enter the term : 5
Enter First Number : 1
Enter Second Number :2


Fibonacci Series : 1 2 3 5 8