Design & Develop and Implement a Program in C for Solving Tower of Hanoi problem with n disks

Solving Tower of Hanoi problem with n disks:

The Tower of Hanoi is a mathematical game or 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, thus making a conical shape.

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. With three disks, the puzzle can be solved in seven moves.
The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n – 1, where n is the number of disks.

void tower(int n, int source, int temp,int destination)
if(n == 0) 
tower(n-1, source, destination, temp); 
printf("\nMove disc %d from %c to %c", n, source, destination); 
tower(n-1, temp, source, destination); 
void main() 
int n;
printf("\nEnter the number of discs: \n"); 
scanf("%d", &n);
tower(n, 'A', 'B', 'C'); 
printf("\n\nTotal Number of moves are: %d", (int)pow(2,n)-1); 
Enter the number of discs:
Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B
Move disc 3 from A to C
Move disc 1 from B to A
Move disc 2 from B to C
Move disc 1 from A to C
Total Number of moves are: 7"

Leave a Reply