Tower of Hanoi
Tower of Hanoi is a program where disks are transffered from one peg to another. We have used three pegs where one is used as auxiliary. We transfer the disks one by one like we do it in stack.
#include<stdio.h>
#include<conio.h>
void tower(int n,char frompeg, char topeg,char auxpeg)
{
/* if only one disk,make tho move and turn */
if(n==1)
{
printf("\n move disk 1 from peg %c to peg %c",frompeg,topeg);
return;
}
/* Move top n-1 disks from A to B using C as auxiliary */
tower(n-1,frompeg,auxpeg,topeg);
/* Move remaining disk from A to C */
printf("\n move disk %d from peg %c to peg %c",n,frompeg,topeg);
/* Move n-1 disk from B to C using A as auxiliary */
tower(n-1,auxpeg,topeg,frompeg);
}
void main()
{
clrscr();
int n;
printf("enter :");
scanf("%d",&n);
tower(n,'A','C','B');
getch();
}
Download Program


