I need a C program that implements fibonacci series using recursion.. I am just getting wrong outputs.. can anyone post the program code for it?

thanks in advance..

rahul

- Thread starter Rahul++
- Start date

I need a C program that implements fibonacci series using recursion.. I am just getting wrong outputs.. can anyone post the program code for it?

thanks in advance..

rahul

Try this , its C++

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int n,m,i,current,next,twoaway;

cout<<"How many numbers do you want to compute?!: ";

cin>>n;

cout<<"Series will start in what number?!: ";

cin>>m;

if (n<=0||m<=0) cout<<"number should be positive!";

else {

next=current=m;

for (i=1;i<=n;i++)

{

cout<<" "<<current<<" ";

twoaway=current+next;

current=next;

next=twoaway;

}

}

getche();

}

there are also few tutorial you should check

HowStuffWorks "How C Programming Works"

C Tutorial

codereverser said:try this algo (not in any specific language):

Code:`function fib(n) { if n <= 1 : return n else return fib(n-1) + fib(n-2); }`

There u go !!!!!!!!!!!!AceMcCloud said:Here...this might help...

int fib(int n)

{

if (n <= 1)

return n;

else

return fib(n-1)+fib(n-2);

}

Recursion programs are quite easy to code and also are small. If you are getting errors, try to find out what the error is. If you have the logic right, most probably it would just be a small silly mistake you have made. Simply copy pasting will do absolutely no good.

For the members who pasted the complete source, think about it. Dont help lazy bums who beg for their homework. They will learn nothing from it, and keep coming again asking for the complete code.

I wanna print 7 terms of fibonacci series ie;

1 1 2 3 5 8 13

I couldn't figure this one out..

Alright let me give a hint...

You already have an example of a recursive program posted here, so if you want to print the actual sequence in the ascending order, all you need to do is to tweak the recursion such that it generates the fibonacci number in the ascending order and print the number at each step. You already know the first two numbers of the sequence are 0 and 1. so use them for starting the recursion.

Next if you want to control how many numbers of the sequence you print, just use an additional 'counter' parameter in the function which you decrement every time you enter the recursive function. make the function return when the counter reaches 0.

I know how to do it if we reverse the recursion. I was asking how to just modify this existing code to get that output. I think its not possible, eh?Lord Nemesis said:

Alright let me give a hint...

You already have an example of a recursive program posted here, so if you want to print the actual sequence in the ascending order, all you need to do is to tweak the recursion such that it generates the fibonacci number in the ascending order and print the number at each step. You already know the first two numbers of the sequence are 0 and 1. so use them for starting the recursion.

Next if you want to control how many numbers of the sequence you print, just use an additional 'counter' parameter in the function which you decrement every time you enter the recursive function. make the function return when the counter reaches 0.

Here's a simple program for you. I assume its simple but I just couldn't figure out how.

Print the sequence till n number of terms without using any conditions and using just one printf command in whole program. And yeah, no arrays.

Sorry, I am just plain stupid.

UPDATE: I solved according to the requirements...