0
Can anyone tell the code to print the Fibonacci series ?
2 Answers
+ 4
int main()
{
int a=0,b=1,i,j, c;
cout<<"enter terms";
cin>>i;
cout<<a<<"\t"<<b<<"\t";
for(j=0;j<i-2;j++)
{
c=a+b;
a=b;
b=c;
cout<<c<<"\t";
}
return 0;
}
0
Using recursion:
#include <iostream>
using namespace std;
int fibonacci(int n)
{
if((n==1)||(n==0))
return(n);
else
return(fibonacci(n-1)+fibonacci(n-2));
}
int main()
{
int n;
cin >> n;
for(int i = 0;i < n;++i)
cout << fibonacci(i) << endl;
return 0;
}