Inlcude required header files
#include<iostream.h>
#include<conio.h>
Function Declaration
void fibonacci(long int i,long int j);
Instance Variables Declaration
static int totNum;
static int count;
Main Method which uses the recursive function to print fibonacci series
void main(){

Local variables declaration

long int i=0,j=1;
clrscr();

Accept the max number of series

cout<<"Enter The Limit:";
cin>>totNum;

Print initial numbers

cout<<endl<<i<<" "<<j<<" "; 

Track count, initialize to 2, as the starting two numbers are already printed.

count=2;

Recurse through function to print series

fibonacci(i, j);
getch();
}

Now the actual funtionality to get the fibonacci number reccursively.

void fibonacci(long int i,long int j){
// Print next number
cout<<i+j<<" ";

// Increase the count
count++;

Check if count reaches the max number else recurse.

 if(count<=totNum){
fibonacci(j,i+j);
}
}

Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments