Java Program for Tower of Hanoi Problem

Source of Tower of Hanoi has been after Tower of Brahma custom. Story begins like this, in an antiquated sanctuary of Kashi which contains a huge room with three towers in it encompassed by 64 golden disks.

At the time of formation of the world, Brahma made a precious stone tower with 64 discs made of gold in it. The disks were of decreasing size and were stacked on the tower in an order (the biggest will be at the base and the following littler in size will be over the keep going etc).

Other than this tower there were two other towers. Since the creation, Brahman ministers have been endeavoring to move the circles from tower A to tower C utilizing tower B for middle stockpiling.As the disks are very heavy, they can be moved only one at a time. 

Likewise, at no time can a bigger plate be on top of a littler circle. As indicated by Brahma, the world will reach an end when the ministers have finished their errand. 

The most captivating thing about this riddle is the legend can be demonstrated deductively genuine. On the off chance that the ministers had the capacity to move circles at a rate of one for each second, utilizing the littlest number of moves, it would take them (2^64)-1 seconds (same no. of moves) or about 585 billion years to complete, which is around 127 times the present age of the sun.

Java Program for Tower of Hanoi Problem

Before we dissect the project, I would like to give few attributes with respect to the calculated model of the towers. I’ve picked utilizing a non-recursive model as it would be somewhat easy to clarify and get it. 

We have three towers named t1, t2, and t3.They are represented utilizing Arrays such that the first element of every tower is zero. On the off chance that the tower is unfilled it will be comprising of all 0’s.

You can look at the image to grab a better perspective of the towers.

Java Program for Tower of Hanoi Problem

I declared a class for it that exposes methods to swap discs between the towers, print the status of each tower and to initialize the towers.

First is the initialization of towers which will be accomplished by the parameterized constructor as shown below. The starting tower consists of all the discs in order hence it is a separate case.


The next part is just swapping all the discs until we transfer all the discs from tower 1 to tower 3.

This will be done by the following if else loop.


Actually if you have observed I’ve used a sequence here for even and odd number of discs.

Algorithm for Swapping Discs

The perfect algorithm for moving the towers is given below.

For an even number of discs

1. Make the move of disc between t1 and t2. 
2. Make the move of disc between t1 and t3. 
3. Make the move of disc between t2 and t3. 
4. Repeat until complete. 

For an odd number of discs 

1. Make the move of disc between t1 and t3. 
2. Make the move of disc between t1 and t2. 
3. Make the move of disc between t3 and t2. 
4. Repeat until complete.

The final program is given below.


Sample Output

Java Program for Tower of Hanoi Problem

Leave a Comment

Your email address will not be published. Required fields are marked *