//C program to implement tower of hanoii
#include<stdio.h>#include<conio.h>
void tower(int,char,char,char);
void main()
{
int n;
clrscr();
printf("Enter number of disc : ");
scanf("%d",&n);
//move source to target pole using Intermediate pole
tower(n,'S','I','T');
getch();
}
void tower(int n,char s,char i, char t)
{
if(n>=1)
{
// move n-1 discs to intermediate pole using target pole as intermediate pole
//so finally we will have large disc in source so print move disc from source to target
tower(n-1,s,t,i);
printf("\nMove %c to %c",s,t);
//intermediate pole has n-1 discs we have to move these discs to target using source pole as intermediate
tower(n-1,i,s,t);
}
}
Output
No comments:
Post a Comment