Tower of Hanoi consists of three towers called as pegs with n number of rings. Rings are of different size.
Step-3: move n-1 rings from auxiliary tower to the tower where the rings are required to be placed.
Conditions to be fulfilled:
The basic conditions which are required to be fulfilled are as follows :
- It is required to move one ring at a time.
- The smaller ring should not lie below the larger ring.
Steps to be performed :
Steps-1: move n-1 ring to the auxiliary tower.
Step-2: move the last ring in the first tower to the tower where the rings are required to be placed.
Step-3: move n-1 rings from auxiliary tower to the tower where the rings are required to be placed.
Algorithm :
towerOfHanoi ( ringsCount, from tower, to tower, aux tower){ if(ringsCount==1) { print(from tower , to tower); return; } towerOfHanoi(ringsCount, from tower, aux tower, to tower); print(from tower , to tower); towerOfHanoi(ringsCount, aux tower, to tower, from tower); } |
Coding In Java:

