# Fibonacci series using recursion in C

#### Rahul++

##### SuperUser
Hello,

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?

rahul

#### Scorpy

##### Active Member
What does using recursion mean ? I know how to print it using 1 for loop

#### bombaybaba

##### Active Member
If i am not wrong in simple words , a function calling itself is recursion ??

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();

}

#### codereverser

##### Well-Known Member
Disciple
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);

}``````

1 person

#### AceMcCloud

##### New Member
Disciple
Here...this might help...

int fib(int n)

{

if (n <= 1)

return n;

else

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

}

1 person

#### harryneopotter

##### NEO is my middle name
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);
}``````
AceMcCloud said:
Here...this might help...
int fib(int n)
{
if (n <= 1)
return n;
else
return fib(n-1)+fib(n-2);
}
There u go !!!!!!!!!!!!

#### viralbug

##### Well-Known Member
You really shouldn't ask to be spoon fed.

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.

1 person

#### dennis_nail

##### Well-Known Member
viralbug is absolutely correct. You would learn a lot by trying and making mistakes, than getting readymade source code from somewhere. But in most cases, we realize this only when we have to do programming for a living.

##### New Member
+1 for what virulbug said. People should be ashamed of asking the complete source code. You will learn nothing from it. At least give it a try first, if you dont know what recursion is, ask what it is, read about it. Then come.

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.

#### akhurana

##### New Member
use dynamic programming :O

#### street.soul

##### New Member
Disciple
+1 for viral bug . Users should pos their code which contains errors and then only we should help them. Diretly giving out the code is no point and wont help at all in learning.

#### booo

##### BA BA BA BABANANA
Veteran
did you try debugging your code?

@all other ppl here: stop helping with homeworks.

#### sabby

##### Well-Known Member
Section Mod
@OP

rather copying source codes from here, post your source code here so that we can find out the mistakes/errors. This will really help you.

#### triple s

##### Well-Known Member
Disciple
Agree with Sabby!!!!!

#### dpacmittal

##### New Member
Disciple
what if you don't want the sum, just the sequence to be printed.. like

I wanna print 7 terms of fibonacci series ie;

1 1 2 3 5 8 13

I couldn't figure this one out..

#### Scorpy

##### Active Member
dude even I have to print that and Im figuring out how .... the only method I know right now is using for loops and doing it the way the first guy showed :/ ....

#### Lord Nemesis

##### Overlord
Veteran
^^ LOL... read what viralbug and hammerhead said again. Just give it a try and you will get it.

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.

#### Scorpy

##### Active Member
ZZZZ Im not getting it <.> .... i guess my weak mind cannot understand the simple logic

#### dpacmittal

##### New Member
Disciple
Lord Nemesis said:
^^ LOL... read what viralbug and hammerhead said again. Just give it a try and you will get it.

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?

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...