C++ Program to Print Fibonacci Series Till N
#include <iostream>
using namespace std;
int main()
{
int i,n,n1=0,n2=1,n3;
line();
cout<<"Fibionacci series Till N\n";
line();
cout<<"Enter N: ";
cin>>n;
line();
for(i=1;i<=n;i++)
{
cout<<n1<<", ";
n3=n1+n2;
n1=n2;
n2=n3;
}
cout<<"\b\b ="<<n3<<endl;{
int n,n1=0,n2=1,nextTerm=0;
cout<<"Enter the number of terms: ";
cin>>n;
cout<<"Fibonacci Series: ";
for(int i=1;i<=n;++i)
{
// Printing the first two terms.
if(i == 1)
{
cout<<n1<<", ";
continue;
}
if(i == 2)
{
cout<<n2<<", ";
continue;
}
nextTerm=n1+n2;
n1=n2;
n2=nextTerm;
cout<<nextTerm<<", ";
}
return 0;
}
Comments
Post a Comment