C program for the Tower of Hanoi is an implementation of the easy game puzzle in c programming.

Tower of Hanoi
Tower of Hanoi is a puzzle and mathematical puzzle which contains 3 rods with disks placed on then from largest to small and we need to move disks from source to destination using the temporary rod.

How the tower of Hanoi works
Tower of Hanoi game: We have 3 rods (tower). And disks with different sizes arranged on the rod in ascending order such that the largest one is on the bottom.
The mission is to move all the disks to some other tower without violating the sequence of arrangement.

Tower of Hanoi rules

1. Only one disk is moved from the rod at a time
2. No large disk can be placed on a small disk
3. The only topmost disk can be moved.

Tower of Hanoi example

1. Take 3 pegs (tower)
2. First, we move the smaller (top) disk to aux peg.
3. Then, we move the larger (bottom) disk to the destination peg.
4. And finally, we move the smaller disk from aux to destination peg.

Page Contents

## Tower of hanoi algorithm

1. Take 3 peg (source, destination, auxiliary)
2. Move n-1 disks from source to aux
3. Next Move nth disk from source to dest
4. Move n-1 disks from aux to dest

## C program for tower of hanoi

```#include <stdio.h>
void towerOfHanoi(int,char,char,char);
main()
{
int n;
printf("Enter no of disks\n");
scanf("%d",&n);
towerOfHanoi(n, 'A', 'C', 'B');
}

void towerOfHanoi(int n, char from, char to, char temp)
{
if (n == 1)
{
printf("Move disk 1 from %c to %c\n", from, to);
return;
}
towerOfHanoi(n-1, from, temp, to);
printf("Move disk %d from %c to %c\n", n, from, to);
towerOfHanoi(n-1, temp, to, from);
}```

Output :