# Tower of Hanoi Algorithm C Program

## 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.

**Also Read: C Program To Print Fibonacci Series using Recursion**

#### 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, **2 ^{n} – 1** moves or disk shift are required.

**Also Read: Tower of Hanoi without Recursion in C Programming**

#### Code For Tower of Hanoi Algorithm in C Programming using Recursion

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include<stdio.h> 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); return 0; } 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); return 0; } int main() { char source_tower = 'A', temporary_tower = 'B', destination_tower = 'C'; int limit; printf("\nEnter The Number of Disks:\t"); scanf("%d", &limit); printf("\nSequence of Disks:\n"); tower_of_hanoi(limit, source_tower, temporary_tower, destination_tower); printf("\n"); return 0; } |

**Also Read: C Program To Generate Floyd’s Triangle**

#### Output

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.

Fantastic. You made it so simple implementation to understand.

Can we solve this Tower of Hanoi in C without using Recursion?

It would be a poor solution for problems where the number of disks is high

Yes. We can definitely solve Tower of Hanoi problem without Recursion. Any recursive program can be converted into a non recursive program that uses iterative for loops. I ve found the same program on this site.

Why do we have to use Recursion? Can’t we use For loop instead of Recursion in this C program to solve tower of hanoi problem? Recursion is bit confusing.

Are the Temporary Tower and Auxilary Towers same?

Yes. They both are just one and the same. It’s just another name for the Temporary Tower.

Thanks for the Explanation. Tower of Hanoi Problem seems to be so interesting.

This is such a shortcode for Tower of Hanoi Problem Algorithm. Really good and simple code.

This is probably the most shortest code for tower of hanoi problem in c programming. Thanks!

Does this Tower of Hanoi algorithm use Stacks?

Yes. It is based on stacks only if you see the diagram above.

Tower of Hanoi is one of the best problem to understand recursion in C programming.

That means, for 3 disks on the tower, there will be 7 movemebts amongst source, auxillary and destination tower. Since, 23 – 1 = 7.

Tower of Hanoi is an NP Problem which indicates Non Deterministic Polynomial Time.

To be more specific, it is an NP – Complete Problem.

In this TOH C Program, we have three rods and n disks. Is it possible to increase the number of towers/rods and then formulate this problem?

बहुत बड़िया भाई। शुकृया।

I haven’t ever seen an explanation better than this for tower of hanoi program. Thanks a lot.

How to trace the number of moves using recurssion ?