/*C++ Program to Print Fibonacci Series*/
#include<iostream>
using namespace std;
int main()
{
int n,x=0,y=1,next,i;
cout<<"Enter the number of terms : ";
cin>>n;
cout<<endl<<"Fibonacci series : ";
for(i=1;i<=n;i++)
{
if(i==1)
{
cout<<" "<<x;
}
if(i==2)
{
cout<<" "<<y;
}
if(i>2)
{
next=x+y;
cout<<" "<<next;
x=y;
y=next;
}
}
return 0;
}
OUTPUT:
Enter the number of terms : 10
Fibonacci series : 0 1 1 2 3 5 8 13 21 34
/*C++ Program to Print Fibonacci Series upto Certain Number*/
#include<iostream>
using namespace std;
int main()
{
int n,x=0,y=1,next=0,i;
cout<<"Enter the number : ";
cin>>n;
cout<<endl<<"Fibonacci series : ";
next=x+y;
while(next<=n)
{
if(i==1)
{
cout<<" "<<x;
}
if(i==2)
{
cout<<" "<<y;
}
if(i>2)
{
cout<<" "<<next;
x=y;
y=next;
next=x+y;
}
i++;
}
return 0;
}
OUTPUT:
Enter the number : 100
Fibonacci series : 1 2 3 5 8 13 21 34 55 89
0 comments:
Post a Comment