Fibonacci Number program in C++ using Recursion

0
834

This program shows A Fibonacci Sequence starting from a user’s entered number, the approach used here is recursive.Have a look at the code:

[cpp] //febonacci using recursion
#include<iostream>
using namespace std;

int F(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return ((F(n-1)+F(n-2)));

}
int main()
{
int num;

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

cout<<F(num)<<endl;

}
[/cpp]

A recursive function is created which takes the user’s entered number as an argument and uses it for finding the next values.