Showing posts with label recursion. Show all posts
Showing posts with label recursion. Show all posts

Saturday, November 19, 2011

Find Fibonacci Series Using Recursion in C Programming

It is sample C program which demonstrate how to use recursion concept in c programming. Here is a recursion program which accept a number from user and generate Fibonacci series using recursion.

Program Code
//Find Fibonacci Series Using Recursion in C Programming
#include"stdio.h"
#include"conio.h"
fibonacci(int n);
void main()
{
 int n,r,i;
 clrscr();
 printf("********** Fibonacci Series Using Recursion *********\n");
 printf("enter a number: ");
 scanf("%d",&n);
 printf("\n\n");

 for(i=0;i<=n;i++)
 {
  r=fibonacci(i);
  printf("\t%d",r);
 }
 getch();
}

fibonacci(int n)
{
 int f;
 if((n==1)||(n==2))
  return(1);
 else if(n==0)
  return(0);
 else
  f=fibonacci(n-2)+fibonacci(n-1);
 return(f);
}
Output