The Towers of Hanoi is a mathematical game or
puzzle. The objective is to move the all of the discs one at a time from an
arbitrary peg to another. Putting a larger disc over a smaller one must be
avoided at all times and the transfer must be made in the least possible moves,
which is 2^n - 1, where n is the number of disks. For example, the minimum
number of moves required to move three discs are 2^3 - 1 = 7.
#include <stdio.h>
void towers(int, char, char, char);
int main() {
int num;
printf("Enter the number of disks: ");
scanf("%d", &num);
printf("\nThe sequence of moves involved in the
Tower of Hanoi are: \n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char
auxpeg)
{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg
%c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg
%c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}