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

}

}

40 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

Anonymous 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

Anonymous 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

Anonymous said...

To know the programming technique: "Make Your Program Trial Version in C" visit:
udebayan.blogspot.com

Aptech yelahanka said...

This blog is awesome,thanks for ur valuable inforamtion ,u want more inforamtion about C programming languages join C programming classes in bangalore,visit : Aptech computer education,my tage are :
C training bangalore,C courses bangalore,C classes bangalore,C institute bangalore,C coaching centers bangalore,C programming institute in bangalore,C programming classes in bangalore

Md Razu Ahmed said...




What you get from RSC Bux - Main Benefits :
Reach millions of clients
Easy management
Demographic filter
Affordable rates
Anti-cheat protection
Detailed statistics


bux
the rsc bux
advertising
perfect money
get Free Money Online

Nikithar said...

Thank you for posting the great content……I found it quiet interesting, hopefully you will keep posting such blogs…We are the best share market tips provider and we are providing best copper tips

kaurn said...

Very Useful Post!Thanks!
C Programming

Course finder said...

Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital informationC Programming Training in Chennai

Anonymous said...

The program is to be written in Matlab. Program must be liberally commented.
The program is a game played by a human versus the machine.
At the start, the prompt asks the human to enter three non-zero integers. These will constitute how many counters are in each Pile.
The machine then asks who goes first.
A turn consists of selecting a Pile and taking a number of counters from that pile.
For example, if the Piles contain 3, 7 and 12 counters, a legal move consists of taking from 1 to 3 from Pile 1, or 1, 2, … 7 from Pile2 etc.
The object of the game is to be the player to take the last counter(s). So if Piles contain 0 2 0, the player whose turn it is will take 2 from Pile 2 and win.
The program must play and optimal strategy so if it has a winning position it will win the game. If it does not have a winning position it should try to extend the game.
At the end of the game, the program must declare the winner.