Get Email Subscription

Enter your email address:

Delivered by FeedBurner

Wednesday, April 21, 2010

This program generates fibonacci series in a recursive manner. (C++)


This program generates fibonacci series in a recursive manner. (C++)
 
This program is very famous among interviewers and will be asked in technical interviews. 
 
 
 
#include< iostream.h>
#include< conio.h>
long fibo_number(int );
long fibo_number(int n)
{
int r;
if((n==1)||(n==0))
return 0;
else
if(n==2)
return 1;
else
return(fibo_number(n-1)+ fibo_number(n-2));
}
/* main function */
main()
{
int n;
int i=0;
cout< < endl< < " Input the number for fibonacci series, you want to generate:";
cin>> n;
cout< < "\n Fibonacci Series is as follows: \n";
while(i< n)
{
++i;
cout< < " "< < fibo_number(i));
}
}
 
 
 
 
--------------------------------------------------------------------------------
 
 
The output of the program:
 
 
Input the number for fibonacci series, you want to generate:12
 
Fibonacci Series is as follows:
0 1 1 2 3 5 8 13 

Related Posts by Categories



0 comments:

About This Blog

Lorem Ipsum

  © Blogger templates Newspaper III by Ourblogtemplates.com 2008

Back to TOP