Program To Solve The Mystery of Towers of Hanoi Using Recursion
The famous Towers of Hanoi problem has an elegant solution through recursion. In the problem, three disk A, B and C exist. ‘n’ disks o...
The famous Towers of Hanoi problem has an elegant solution through recursion. In the problem, three disk A, B and C exist. ‘n’ disks of different diameters are placed on pillar A.
The objective is to move all the disk to pillar C using pillar B as auxiliary.
The conditions for the game are -
#include<stdio.h>
int toh(char a,char b,char c,int n)
{
/* If only 1 disk, make the move and return */
if(n==1)
{
printf("\n\t %c->%c\n",a,c);
}
else
{
toh(a,c,b,n-1);
toh(a,b,c,1);
toh(b,a,c,n-1);
}
}
int main()
{
int n;
printf("Enter The Range - " );
scanf("%d",&n);
printf("\nThe Tower of Hanoi - \n");
toh('a','b','c',n);
return 0;
}