Tower of Hanoi without Recursion C Program

By | July 30, 2016

C Program To Solve Tower of Hanoi without Recursion

Learn How To Solve Tower of Hanoi without Recursion in C Programming Language. This Non Recursive C Program makes use of an Iterative method using For Loop to solve Tower of Hanoi Problem. The Tower of Hanoi Algorithm in Data Structures is a very famous Interview Question for Beginners. The C Program For Tower of Hanoi Program using Iteration can be solved by using For, While and Do While Loop.

To know more about Tower of Hanoi, you can read this guide: Tower of Hanoi Problem in C Programming

If you try to compile this C Program for Tower of Hanoi without using Recursion in Linux, you will get the following error:

This is because the pow() method cannot be found in the library files. To overcome this error, you will have to explicitly include the math.h header file. Compile the program using the following command:

Also Read: Tower of Hanoi in C using Recursion

C Program To Solve Tower of Hanoi without Recursion

Must Read: C Program For FCFS Algorithm

Output

If you have any compilation errors or doubts in this C Program for Tower of Hanoi without Recursion, let us know about in the Comment Section below.

5 thoughts on “Tower of Hanoi without Recursion C Program”

1. c programmer

Here’s another method to solve the Tower of Hanoi puzzle.

It’s a mechanical solution which doesn’t use recursion. Try it out using
3 or 4 coins of different sizes.

Arrange the three rods to form a triangle.

Starting position (where X, Y and Z are different size coins):

empty rod

Z
YYY
XXXXX

starting rod destination rod

Finished position:

empty rod

Z
YYY
XXXXX

starting rod destination rod

Move the smallest disk on every other turn — always in the same
direction. On the remaining turns make the only valid move that does
not involve the smallest disk.

The following rule will make sure that the tower of disks end up on the
third rod: If the number of disks in the puzzle is an odd number then
always move the smallest disk counter-clockwise around the triangle; if
the number of disks in the puzzle is an even number then always move the
smallest disk clockwise around the triangle.

With this solution the even numbered disks move around the triangle in
one direction while the odd numbered disks move around the triangle in
the opposite direction.

1. Tushar Soni Post author

Thanks a lot for providing us a different angle to Tower of Hanoi Problem in C Language.

2. Rohan Kush

I think Recursion is much better instead of iterations since the recursive tower of hanoi algorithm is much simple to understand and looks efficient as well.

3. Rohan Gaikwad

Tower of Hanoi with Iteration method is much more understandable than the Recursive approach. Thanks!

4. Vinay Gupta

Thanks for the code iterative code for Tower of Hanoi. Thanks