Showing posts with label Recursive Programs. Show all posts
Showing posts with label Recursive Programs. Show all posts

Friday, 10 January 2014

C Program to print Tower of Hanoi using recursion !!

#include<stdio.h>
#include<conio.h>
void TOH(int n,char x,char y,char z);
void main()
{
 int n;
 printf("nEnter number of plates:");
 scanf("%d",&n);
 TOH(n-1,'A','B','C');
 getch();
}
void TOH(int n,char x,char y,char z)
{
 if(n>0)
 {
  TOH(n-1,x,z,y);
  printf("n%c -> %c",x,y);
  TOH(n-1,z,y,x);
 }
}
Following Image will explain you more about tower of hanoi :

Thursday, 9 January 2014

Find Factorial of Number Using Recursion

#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
 int x,n;
 printf("nEnter the value of n :");
 scanf("%d",&n);
 x=fact(n);
 printf("n%d",x);
 getch();
}
int fact(int n)
{
 if(n==0)
  return(1);
 return(n*fact(n-1));
}