#include<stdio.h>
void TOH(int n,char from,char to,char aux);
void main()
{
int n;
printf("No. of disks: ");
scanf("%d",&n);
printf("Sequence of moves for %d disks of TOH",n);
TOH(n,'A','C','B');
}
void TOH(int n,char from,char to,char aux)
{
if(n==1)
{
printf("\nMove disk %d from %c rod to %c rod ",n,from,to);
return ;
}
TOH(n-1,from,aux,to);
printf("\nMove disk %d from %c rod to %c rod ",n,from,to);
TOH(n-1,aux,to,from);
}
Note: Need to be arranged in compiler after copied
OutPut:
No. of disks: 3
Sequence of moves for 3 disks of TOH
Move disk 1 from A rod to C rod
Move disk 2 from A rod to B rod
Move disk 1 from C rod to B rod
Move disk 3 from A rod to C rod
Move disk 1 from B rod to A rod
Move disk 2 from B rod to C rod
Move disk 1 from A rod to C rod