Towers of Hanoi - Part 2

Collapse Content

We just covered the Towers of Hanoi for Case 1 and Case 2, let's return to 3 disks. Can you describe an algorithm to move the 3 disks from a starting peg to the goal peg? You can refer back to solved cases when needed.

The Algorithm for 3 Disks

Now that we've covered 1, 2 and 3 disks, can you figure out an algorithm for solving the towers of Hanoi for any number of N disks?

towers of turtles

The Algorithm for N Disks


Create a program that solves the tower of Hanoi. You will be given one number as input - the number of disks on Peg 1. Create and call a method that prints out the correct steps to solve the puzzle.

Output Format: Print the peg to move from, an arrow "->", and the peg to move to. For example, to move from peg 1 to peg 3, print:

Print all the steps for a given case on its own line.


Create a program that solves the Towers of Hanoi and prints out the solution.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Contact Us