# C Program for Tower of Hanoi

The tower of Hanoi is a mathematical puzzle. It consists of three rods and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top. We have to obtain the same stack on the third rod.

The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules−

• Only one disk can be moved at a time.

• Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.

• No disk may be placed on top of a smaller disk.

## Sample

Input  − 3

Output   − A to B

A to C

B to C

A to B

C to A

C to B

A to B  Explanation − uses recursive function & solves the tower of Hanoi.

## Example

#include<stdio.h>
void TOH(int n,char x,char y,char z) {
if(n>0) {
TOH(n-1,x,z,y);
printf("%c to %c",x,y);
TOH(n-1,z,y,x);
}
}
int main() {
int n=3;
TOH(n,'A','B','C');
}

## Output

A to B
A to C
B to C
A to B
C to A
C to B
A to B