# Tower of Hanoi – C Program

# Tower of Hanoi C Program

It is basically a mathematical puzzle game.

Suppose there are 3 sticks A,B & C and suppose on stick A, there are finite number (n) of disks with decreasing size, the objective of the game is to move the disk from stick A to stick C using stick B as auxiliary obeying some rules.

**Rules of the game are as follows:**

**Rule 1:** Only 1 disk can be moved at at time, specifically only the top disk on any stick may moved to any other stick.

**Rule 2:** At no time can a larger disk can be placed on a smaller disk.

Sometimes we will write X->Y to denote the instruction move the top disk from stick X to stick Y where X & Y may be any of the three sticks.

## Program of Tower of Hanoi:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<stdio.h> #include<conio.h> t_hanoi(n,left,mid,right) { if(n!=0) { t_hanoi(n-1,left,right,mid); printf("\n%c->%c",left,right); t_hanoi(n-1,mid,left,right); } } void main() { int n; clrscr(); printf("\nenter the number of disk"); scanf("%d",&n); t_hanoi(n,'A','B','C'); getch(); } |

In the above program, A, B & C are three sticks.