# Fibonacci series using Recursion

Saurabh | Feb 08, 2010 | Comments 10

As a simple rule of recursion, any function can be computed using a recursive routine if :

1. The function can be expressed in its own form.

2. There exists a termination step, the point at which f(x) is known for a particular ‘x’.

Therefore to write a recursive program to find the nth term of the fibonacci series, we have to express the fibonacci sequence in a recursive form using the above 2 rules :

1. fib(n) = fib(n-1) + fib(n-2) (recursive defination of fibonacci series).

2. if n=0 or n=1, return n (termination step).

Using these 2 rules, the recursive program for finding the nth term of the fibonacci series can be coded very easily as shown.

The exe file can be downloaded from here : FIBONACCI

```
#include "stdio.h"
#include "conio.h"
int y;
fib(int n)
{ if(n==1 || n==0)
return n;
y=fib(n-1)+fib(n-2);
return y;
}
main()
{ int a,r;
clrscr();
printf("Enter any number : ");
scanf("%d",&a);
r=fib(a);
printf("The no. at position %d is %d",a,r);
getch();
return 0;
}
```

**About the Author**: Software Engineer - Advanced Search & Recommendation at Rovi

Awesome work. I always find very unique posts here on your Blog.

nice work,

but a small variation is advisable….

ie,

return n+1 if the value of n is 0 or 1. This is because fibonacci series is generally regarded to begin with 0.

the fibonacci series is

0 1 1 2 3 5 8 13………….

so according to your logic, if you give 3 as input, the result displayed will be 2 which is wrong.

#include

#include

int Fib(int x, int range);

int main(){

int r;

printf(“Enter the MAx range for Fibonacci Series “);

scanf(“%d”, &r);

Fib(r,r);

printf(“\n”);

}

Fib(int x, int range )

{

static int sum=0,pre=0,p=1;

if (sum>=range)

return 0;

else

{

if (pre==0 && p==1 && sum==0)

{sum=pre+p;

printf(“%d %d %d”, pre,p,sum);

}

else

{ pre=p;

p=sum;

sum=pre+p;

printf(” %d “, sum);

}

}

Fib(sum,range);

}

thanks alot!! it came handy.

/*Simple c program for Fibnocci series with Recurion

By Tintu.C.Raju

*/

#include

#include

void fib(int n,int f1=0,int f2=1)

{

if(n)

{

printf(“%d “,f1);

fib(n-1,f2,f1+f2);

}

else return;

}

void main()

{

int n;

printf(“\n\tEnter the number\n”);

scanf(“%d”,&n);

printf(“\n\tThe Fibonacci series using recursion is\n\t”);

fib(n);

getch();

}

thank u so much for this algorithm and it is very simple to understand ….

pleas help me about operation on queue algorithm…

Thanks its really nice

guys this is also some easy program made by me for the same Fibonacci series just try i.e. run it once

#include

int fibo(int n)

{

if(n==1||n==0)

return n;

else

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

}

int main()

{

int n,i;

puts(“enter any number”);

scanf(“%d”,&n);

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

{

printf("%d\t",fibo(i));

}

}

Thanks for sharing your thoughts about recursion. Regards

what is the use of series in recursion>?

please explain me:

i cant understand.