#include<iostream.h>
using namespace std;
int main()
{
int f1=0,f2=1,f3,i,n;
cout<<Enter the length of the Series:"<<endl;
cin>>n;
cout<<"Following are the Fibonacci numbers upto nth:"<<endl;
cout<<f1<<endl<<f2;
for(i=2;i<n;i++)
{
f3=f1+f2;
cout<<f3<<endl;
f3=f1;
f1=f2;
f2=f3;
}
return 0;
}
The above program will print the Fibonacci series till to the nth number.
Before getting into the logic of the program we must be knowing what actually is Fibonacci numbers?
So, let me say these numbers are created by summing up the left two values. It starts from 0.
The first two values will be 0 and 1, The addition of these two numbers will get us the third number that is 1, Now, we consider 1 and 1 to get the next digit. So, it is 2.
0 1 1 2 3 5 8 13 21 34...
How we got these numbers?
add 0+1=1, then 1+1=2, then,1+2=3,...it continues.
Output:
Enter the Length of the series:
10
Following are the Fibonacci Numbers till nth:
0 1 1 2 3 5 8 13 21 34
Comments
Post a Comment