Algorithm and Flowchart for Tower of Hanoi


Algorithm for tower of hanoi

Tower of Hanoi, is a mathematical puzzle that consists of three towers. These rings are of varying sizes and stacked upon each other in ascending order. There are other variations of the puzzle where the number of disks increases, but the tower count remains the same.

Source: Wikipedia

A few rules that need to be followed for Tower of Hanoi are -
1.The top disk can be moved only.
2.Only one disk can be moved at a time.
3.No large disk sits above a small disk.

With 3 disks, the puzzle can be solved in 7 moves. The minimal number of moves required to solve a Tower of Hanoi puzzle is 2n - 1, where n is the number of disks.

Flowchart for Tower of Hanoi:

Flowchart for tower of hanoi
Remove WaterMark from Above Flowchart

Algorithm for Tower of Hanoi:

Step 1: Start Step 2: Let the three towers be the source, dest, aux. Step 3: Read the number of disks, n from the user. Step 4: Move n-1 disks from source to aux. Step 5: Move nth disk from source to dest. Step 6: Move n-1 disks from aux to dest. Step 7: Repeat Steps 3 to 5, by decrementing n by 1. Step 8: Stop

In the above algorithm,

  1. Consider three towers source, dest, aux and take value of disks from the user.
  2. Now we move all the above n-1 disks one by one from source to aux.
  3. The largest disk is left at the source, which is then moved to the dest tower.
  4. Then again the rest of the disks are moved back from aux to source.
  5. Again the same steps are repeated so that all the disks are moved to the dest tower.

Similar Posts:




For any inquiry, Contact us at

Facing Programming Errors. Get Answers within 24 hours:

Recommendation Based on your Interest

Quiz For You:
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    Pseudocode for Tower of Hanoi Solution

    Flowchart for Tower Of Hanoi

    Algorithm for Tower of Hanoi

    Tower of Hanoi Algorithm