C Program For Tower of Hanoi Algorithm using Recursion
Learn How To Solve Tower of Hanoi Algorithm in C Programming Language. This C Program For Tower of Hanoi problem using Recursion method is explained below. The Tower of Hanoi Algorithm in Data Structures is a very common Interview Question for Beginners.
Tower of Hanoi Problem Explanation
Towers of Hanoi also known as Lucas’ Tower or Tower of Bramha’s is a mathematical puzzle developed by a Mathematician of French Origin named Édouard Lucas. It is believed that the Solution and Problem for Towers of Hanoi Algorithm was invented by the mathematician in an Indian city in 1883.
What is Tower of Hanoi Problem About?
Tower of Hanoi Algorithm is to move the Disks on the Source Tower to the Destination Tower. But, you should ensure that the Disks on the Destination Tower should be in the same format as in the Source Tower i.e., the Largest Disk should be at the Bottom Position and the Smallest Disk should be at the Top Position.
In other words, a Larger Disk should not be kept on Top of a Smaller Disk. To move these Disks, you can make use of a Temporary Tower , also called as an Auxiliary Tower. A Tower is also frequently called as Peg.
Conditions For Towers of Hanoi Algorithm
- Larger Disk cannot be placed on Smaller Disks
- We can shift only One Disk at a time from One Tower to another.
What is Recursion?
Calling a Function again and again till your Condition is not met is basically Recursion. Recursive Programs tend to Execute a Single Function repeatedly along with its Terminating Condition. It is important to have a Terminating Condition for Recursive Calls so as to prevent from going into an Infinite Loop. Refer this Factorial Program in C Language to understand more about Recursion. We have used Recursion method to solve Towers of Hanoi Algorithm.
How Does Tower of Hanoi Algorithm Work?
The Tower of Hanoi Formula and the Steps For Moving N Disks from Source Tower to Destination Tower:
- Move N-1 Disks from Source Tower To Temporary Tower
- Move Nth Disk from Source Tower To Destination Tower
- Move N-1 Disks from Temporary Tower To Destination Tower (using Source Tower as Temporary Tower)
For a total of n disks, 2n – 1 moves or disk shift are required.
Code For Tower of Hanoi Algorithm in C Programming using Recursion
int tower_of_hanoi(int limit, char source_tower, char temporary_tower, char destination_tower)
if(limit == 1)
printf("\nMove Disk %d From %c To %c\n", limit, source_tower, destination_tower);
tower_of_hanoi(limit - 1, source_tower, destination_tower, temporary_tower);
printf("Move Disk %d From %c To %c\n", limit, source_tower, destination_tower);
tower_of_hanoi(limit - 1, temporary_tower, source_tower, destination_tower);
char source_tower = 'A', temporary_tower = 'B', destination_tower = 'C';
printf("\nEnter The Number of Disks:\t");
printf("\nSequence of Disks:\n");
tower_of_hanoi(limit, source_tower, temporary_tower, destination_tower);
Also Read: C Program To Generate Floyd’s Triangle
If you have any compilation errors or doubts in this Tower of Hanoi in C Programming Language, let us know about in the Comment Section below.