Fibonacci series using recursion in C

Rahul++

SuperUser
Adept
Aug 27, 2007
3,142
175
152
27
Pune
www.wrapcode.com
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?

thanks in advance..

rahul
 

Scorpy

Active Member
Adept
Sep 19, 2009
604
11
32
28
What does using recursion mean ? I know how to print it using 1 for loop :p
 

bombaybaba

Active Member
Adept
Dec 2, 2006
285
6
32
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
Dec 24, 2007
40
11
63
Pune
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);

}
 
  • Like
Reactions: 1 person

harryneopotter

NEO is my middle name
Adept
Dec 24, 2007
728
75
67
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
Adept
Mar 7, 2007
3,054
105
153
29
Navi Mumbai
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.
 
  • Like
Reactions: 1 person

dennis_nail

Well-Known Member
Adept
Jun 4, 2008
650
72
117
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.
 

hammerhead

New Member
Adept
Jun 26, 2006
683
26
0
32
www.teamreflex.org
+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.
 

street.soul

New Member
Disciple
Sep 10, 2009
67
1
0
28
+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
Mar 4, 2008
1,736
1,781
403
Colodaro
did you try debugging your code?

@all other ppl here: stop helping with homeworks.:mad:
 

sabby

Well-Known Member
Section Mod
Jun 13, 2009
2,335
312
123
Bangalore
@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. :)
 

dpacmittal

New Member
Disciple
May 10, 2009
59
1
0
28
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
Adept
Sep 19, 2009
604
11
32
28
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
Jun 3, 2005
5,935
2,403
377
^^ 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
Adept
Sep 19, 2009
604
11
32
28
ZZZZ Im not getting it <.> .... i guess my weak mind cannot understand the simple logic
 

dpacmittal

New Member
Disciple
May 10, 2009
59
1
0
28
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...