Thursday, January 18, 2007

Generation of Fibonacci Sequence using Recursion.

Write a recursive function to obtain the first 25 numbers of a Fibonacci
sequence. In a Fibonacci sequence the sum of two successive terms gives the
third term. Following are the first few terms of the Fibonacci sequence:

1 1 2 3 5 8 13 21 34 55 89 ...



#include<stdio.h>
main()
{

static int prev_number=0, number=1; // static: so value is not lost

int fibonacci (int prev_number, int number);

printf ("Following are the first 25 Numbers of the Fibonacci Series:\n");

printf ("1 "); //to avoid complexity

fibonacci (prev_number,number);


}



fibonacci (int prev_number, int number)

{
static int i=1; //i is not 0, cuz 1 is already counted in main.
int fibo;


if (i==25)
{
printf ("\ndone"); //stop after 25 numbers

}

else
{
fibo=prev_number+number;
prev_number=number; //important steps

number=fibo;

printf ("\n%d", fibo);
i++; // increment counter

fibonacci (prev_number,number); //recursion

}

}

33 comments:

Anonymous said...

Thanks for this example!

MRA
NextDawn C and C++ Tutorials

Copper Skull said...

Thanks. :)

The site you have provided is nice too. :)

siddarth said...

it should be more clear

Tanmoy said...

thanks

gomathi said...

IF THE PROGRAM USED POINTER CONCEPTS THEN IT 'LL BE MOST USEFUL

Echo said...

Thanks a lot!

-Echo
www.echo.net84.net

Anonymous said...

Thanks alot ... it seems to be pretty clear nad would need some more example if possible.
Also I have few info about the same on my blog
c-programming-tutorials.blogspot

prafull said...

thanks a lot sir for your help

loka said...

Thanks for the idea

Anonymous said...

Thanks for the idea

saket said...

thnx... datz gud 1,

surya said...

become a follower for free C,C++ And vb code of my blog:
http://www.a2zhacks.blogspot.com to learn C,C++,VB programming.

Anonymous said...

Thank u

Anooooooooz said...

many many thanx for example....please continue providing more examples in functions

yd said...

thanks.. very easy coding for this program..easily understable..

Anonymous said...

many many thanks for the example

rajat said...

#include
#include
int fabo(int);
int main()
{
int result=0,a=1,b=1,c,i;
printf("enter upto which you want to generate the series");
scanf("%d",&c);
for(i=c-1;i>=0;i--)
{result=fabo(c-i);
printf(" %d",result);}
getch();
}
int fabo(int n)
{
int i;
if (n==1)
return 1;
if(n==2)
return 1;
else
return (fabo(n-1)+fabo(n-2));
getch();
}

Ravi said...

Rajat its good...

diane said...

thanks you so much this helped me a lot.. :D

Joy said...

Thanks for the example very well explained. Infact this site sholud be opened first when the google is surfed

Anonymous said...

the variable a=1 and b=1????is for???

Luv_Addiction said...
This comment has been removed by the author.
Luv_Addiction said...
This comment has been removed by the author.
Jignesh said...

This is awesome post and good imformation
C interview questions

Subha Dasadhikary said...

#include
#include
int fabo(int);
int main()
{
int result=0,c,i;
printf("enter how many no. you see:");
scanf("%d",&c);
for(i=c-1;i>=0;i--)
{result=fabo(c-i);
printf(" %d",result);}
getch();
}
int fabo(int n)
{
int i;
if (n==1)
return 1;
if(n==2)
return 1;
else
return (fabo(n-1)+fabo(n-2));

}

Anonymous said...

find largest database of c programs at

http://www.itstudentjunction.com/c-programs.htm

Sunita said...

Realy good job, i found your blog from google search, I impressed, i bookmark this blog for future

Payday Loans Online | Cash Advance Loans | Fast Cash Loans said...

thanks , i think c is simple and easy language...
Payday Loans Online | Cash Advance Loans | Fast Cash Loans

Chaitanya Reddy said...

http://ccppcoding.blogspot.in/

Anonymous said...

#include
#include

main()

{

char name[|XII+I|];

strcpy(name,"Nemo");

puts(name);

}

Sourabh Kasliwal said...

very simple and cool programs at
http://wininterview.blogspot.in/2013/01/sample-c-programs.html

sanjay said...

Appreciation for nice Updates, I found something new and folks can get useful info about BEST ONLINE TRAINING

aparna john said...

Hi, Nice Program to calculate the factorial of a number. Use the concept of recursion
instead of using loops. .Thanks, its really helped me......

-Aparna
Theosoft