A program to display the following series (i.e. Fibnocci series):

                      0,  1,  1,  2,  3,  5,  8,  13,  21,  .......,  n

where n is the limit of the series entered by the user.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,a=0,b=1,c;

cout<<"Enter a number: ";
cin>>n;

if(n>=0)
cout<<a<<" ";
if(n>=1)
cout<<b<<" ";

do
{
if(n>=1)
{
c=a+b;
cout<<c<<" ";
a=b;
b=c;
}
}
while(c<n);

getch();
}

 

< Back