Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 44: Write a C++ program to print fibonacci series by using recursion.

#include<iostream>
using namespace std;

void fib(int start,int second,int n)
{
	if(n==1)
	{
		cout<<start+second;
	}
	else
	{
		cout<<start+second<<" ";
		fib(second,start+second,--n);
	}
}

int main()
{
	int n,i=0;
	cout<<"The number of terms for fibonacci series:";
	cin>>n;
	cout<<"\nFibonacci series:\n0";

	if(n==2)
		cout<<" 1 ";
	else if(n>2)
	{
		cout<<" 1 ";
		fib(i,i+1,n-2);
	}
	return 0;
}


If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.