Showing posts with label recursion in c. Show all posts
Showing posts with label recursion in c. 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






C Program With Recursion: Find Factorial Using Recursion in C Programming

It is a sample program example which demonstrate recursion in C programming. It is a factorial program with recursion. Accept number from the user and generate factorial of accepted number using recursion.

Program Code
#include"stdio.h"
#include"conio.h"
fact(int n);
void main()
{
 int n,r;
 clrscr();
 printf("************* Find Factorial of Number Using Recursion *************");
 printf("\n enter a number ");
 scanf("%d",&n);
 r=fact(n);
 printf("\n Factorial of %d is =  %2d\t",n,r);
 getch();
}

fact(int n)
{
 int f;
 if((n==0)||(n==1))
  return(1);
 else
  f=n*fact(n-1);
 return(f);
}



Output