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

1. Move N-1 Disks from Source Tower To Temporary Tower
2. Move Nth Disk from Source Tower To Destination Tower
3. 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.

Also Read: Tower of Hanoi without Recursion in C Programming

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

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.

#### Tushar Soni

I am Tushar Soni, Co - Founder of CodingAlpha. I am a computer science student from India and passionate about Web Development and Programming. Connect with me on Facebook | LinkedIn | Google Plus

### 20 thoughts on “Tower of Hanoi Algorithm C Program”

• May 9, 2016 at 2:10 am

Fantastic. You made it so simple implementation to understand.

• July 29, 2016 at 7:38 am

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

• July 30, 2016 at 10:27 am

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

• July 31, 2016 at 1:15 pm

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.

• August 19, 2016 at 12:58 am

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.

• August 29, 2016 at 4:27 pm

Are the Temporary Tower and Auxilary Towers same?

• August 30, 2016 at 8:28 am

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

• September 8, 2016 at 1:28 pm

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

• September 10, 2016 at 12:10 pm

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

• September 29, 2016 at 1:47 pm

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

• October 2, 2016 at 11:07 am

Does this Tower of Hanoi algorithm use Stacks?

• October 2, 2016 at 7:35 pm

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

• October 5, 2016 at 3:39 pm

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

• October 6, 2016 at 4:57 pm

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

• November 2, 2016 at 12:38 pm

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

• November 9, 2016 at 11:11 pm

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

• November 3, 2016 at 1:00 pm

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?

• November 19, 2016 at 10:58 pm

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