WAP to display Fibonacci series (i) using recursion, (ii) using iteration




#include<iostream.h>
#include<conio.h>
class fib
{
int f,s,t;
public:
void fibiteration();
int  fibrecursion(int n);
};
void fib::fibiteration()
{
f=0;
s=1;
int n;
cout<<"enter the number of fib terms you want to print using iteration"<<endl;
cin>>n;
cout<<f<<endl;
cout<<s<<endl;
for(int i=0;i<(n-2);i++)
{
t=f+s;
cout<<t<<endl;
f=s;
s=t;
}
}
int fib::fibrecursion(int n1)
{
if((n1==1)||(n1==2))
return 1;
else
return(fibrecursion(n1-1)+fibrecursion(n1-2));
}

void main()
{
clrscr();
fib obj;
int n;
obj.fibiteration();
cout<<"enter the number of terms of fibonnaci you want to print using recursion"<<endl;
cin>>n;
cout<<"Fibonnaci series using recursion "<<endl;
cout<<0<<endl;
for(int i=1;i<n;i++)
{
cout<<obj.fibrecursion(i)<<endl;
}

getch();
}


Output:- 

Comments