-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Thursday 18 June 2015

Fibonacci series using recursion

Hello and Asalam o Alikum.I hope you have read my previous blogs about recursion in C++.So, today is my last blog about recursion.

Today I will provide you the source code of generating fabonacci series using recursion.Remember that when you planned to find fibanacci series in C++, simply use this method.This method is easier to understand than other methods.

Below is the source code of this program.

Source code


#include <iostream>
using namespace std;

int fibonacci(int num)
{
if(num==0)
return 0;
if(num==1){
return 1;
}
else{
return fibonacci(num-1)+fibonacci(num-2);
}
}

int main()
{
int n;
cout<<"Enter the number of items you want to display: ";
cin>>n;
for(int i=1;i<=n;i++){
cout<<fibonacci(i)<<"\t";
}
return 0;
}

Output

C++,fibonacci series,khgamujtaba,recursion,fibanacci series using recursion

Remember that the basic idea behind solving problems via recursion is to break the instance of the problem into smaller and smaller pieces until the pieces are so small they can be solved trivially. 

No comments:

Post a Comment