Tower of Hanoi Algorithm C Program

By | March 7, 2016

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.

Tower of Hanoi Problem Solution in C Programming
Image Source: MathForum

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


C Program To Solve Tower of Hanoi Algorithm using Recursion

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.

20 thoughts on “Tower of Hanoi Algorithm C Program

  1. Pankaj Dhende

    Fantastic. You made it so simple implementation to understand.

  2. Rajesh Pratap

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

    1. alex

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

  3. Dipak Kale

    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.

  4. Rohan Kush

    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.

  5. Prakash Javdekar

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

  6. Prashank Yadav

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

  7. Raju SAIGAL

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

    1. Vishal Mehta

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

  8. Neha Mishra

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

  9. Mahesh Asalkar

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

  10. Darshana Yadav

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

  11. Sushant Singh

    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?

  12. Monali Thakur

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


Let's Discuss