Saturday, 26 July 2014

C-Routine to implement Towers of Hanoi using Recursive functions

Program

#include<stdio.h>
#include<conio.h>
main()
{
 void hanoi(int,char,char,char);
 int n;
 printf("Enter n");
 scanf("%d",&n);
 hanoi(n,'A','B','C');
}
void hanoi(int n,char p,char q,char r)
{
  if(n==1)
  {
   printf("Move disk %d from tower %c to %c\n",n,p,r);
   return;
  }
  hanoi(n-1,p,r,q);
  printf("Move disk %d from tower %c to %c\n",n,p,r);
  hanoi(n-1,q,p,r);
}

No comments:

Post a Comment