C++ Program to print Fibonacci Series

What is Fibonacci Series?

A series of numbers in which each number is the sum of the two preceding numbers.
For Example:: 1,1,2,3,5,8

Program:

#include<iostream>
using namespace std;
int main()
{
int terms, n1=0, n2=1, next;
cout<<"How many terms of Fibonacci series you want to show? ";
cin>>terms;
cout<<"Fibonacci series:"<<endl;
for(int c=1;c<=terms;c++)
{
if(c==1)
{
cout<<n1<<" ";
}
else if(c==2)
{
cout<<n2<<" ";
}
else 
{
next=n1+n2;
    n1=n2;
n2=next;
    cout<<next<<" ";
}
}
    return 0;
}

Output:



Comments

Popular posts from this blog

C++ Program to take random numbers from user and calculate their sum