Sunday, September 12, 2010

C Program to print Towers Of Hanoi

#include<stdio.h>
#include<conio.h>
long step;
void Hanoi(int n,char from,char to,char spare)
{
   if(n>1)
      Hanoi(n-1,from,spare,to);
      printf(" Step %ld : move #%d %c-->%c\n",++step,n,from,to);

   if(n>1)
      Hanoi(n-1,spare,to,from);
}
void main()
{
   int n;
   printf(" Enter the number of blocks in the tower ");
   scanf("%d",&n);
   step=0;
   Hanoi(n,'A','B','C');
   getch();
}

No comments:

Post a Comment