Contents of page >
 Towers of Hanoi problem with 3 disks in java
 Full Program/SourceCode/Example of Towers of Hanoi problem with n disks in java>
 Now, let’s see Towers of Hanoi problem with 4 disks in java >
 Complexity of Towers of Hanoi problem with n disks in java >
What is the Towers of Hanoi problem>
The Towers of Hanoi is very famous puzzle.
There are three columns(let's say A, B and C) and n number of discs.
We have to transfer all disks from A to C. Keeping in mind that only one disk can only be transferred at a time from one column to another.
Complexity of Towers of Hanoi problem with n disks is 2n1 in java
Towers of Hanoi problem with 3 disks in java >
Step 1 > Towers of Hanoi problem with 3 disks in java >
Step 2 > Towers of Hanoi problem with 3 disks in java >
Step 3 > Towers of Hanoi problem with 3 disks in java >
Step 4 > Towers of Hanoi problem with 3 disks in java >
Step 5 > Towers of Hanoi problem with 3 disks in java >
Step 6 > Towers of Hanoi problem with 3 disks in java >
Step 7 > Towers of Hanoi problem with 3 disks in java >
Full Program/SourceCode/Example of Towers of Hanoi problem with n disks in java >
/**
* Write a program to solve Towers Of Hanoi problem in java
*/
public class TowersOfHanoiAlgorithmExample {
static int stepCount=1;
static int numberOfDisks = 3;
public static void main(String[] args) {
towersOfHanoiSolution(numberOfDisks, 'A', 'B', 'C');
}
public static void towersOfHanoiSolution(int numberOfDisks, char columnA,
char columnB, char columnC) {
if (numberOfDisks == 1) {
System.out.println("Tower of Hanoi  Step = " + (stepCount++)
+ " > Disk 1 " + columnA + " to " + columnC);
} else {
towersOfHanoiSolution(numberOfDisks  1, columnA, columnC, columnB);
System.out.println("Tower of Hanoi  Step = " + (stepCount++)
+ " > Disk " + numberOfDisks + " " + columnA + " to "
+ columnC);
towersOfHanoiSolution(numberOfDisks  1, columnB, columnA, columnC);
}
}
}
/* OUTPUT
Tower of Hanoi  Step = 1 > Disk 1 A to C
Tower of Hanoi  Step = 2 > Disk 2 A to B
Tower of Hanoi  Step = 3 > Disk 1 C to B
Tower of Hanoi  Step = 4 > Disk 3 A to C
Tower of Hanoi  Step = 5 > Disk 1 B to A
Tower of Hanoi  Step = 6 > Disk 2 B to C
Tower of Hanoi  Step = 7 > Disk 1 A to C
*/

So, we wrote program to demonstrate Towers of Hanoi problem in java.
Now, let’s see Towers of Hanoi problem with 4 disks in java >
Step 1 > Towers of Hanoi problem with 4 disks in java >
Step 2 > Towers of Hanoi problem with 4 disks in java >
Step 3 > Towers of Hanoi problem with 4 disks in java >
Step 4 > Towers of Hanoi problem with 4 disks in java >
Step 5 > Towers of Hanoi problem with 4 disks in java >
Step 6 > Towers of Hanoi problem with 4 disks in java >
Step 7 > Towers of Hanoi problem with 4 disks in java >
Step 8 > Towers of Hanoi problem with 4 disks in java >
Step 9 > Towers of Hanoi problem with 4 disks in java >
Step 10 > Towers of Hanoi problem with 4 disks in java >
Step 11 > Towers of Hanoi problem with 4 disks in java >
Step 12 > Towers of Hanoi problem with 4 disks in java >
Step 13 > Towers of Hanoi problem with 4 disks in java >
Step 14 > Towers of Hanoi problem with 4 disks in java >
Step 15 > Towers of Hanoi problem with 4 disks in java >
Complexity of Towers of Hanoi problem with n disks in java >
Complexity of Towers of Hanoi problem with n disks is 2n1 in java
For 3 discs, the number of moves is 231 = 7
For 4 discs, the number of moves is 241 = 15
For 5 discs, the number of moves is 251 = 31
For 10 discs, the number of moves is 231 = 1023
I would recommend you to keep the number of discs down under 20, because once you reach 20 the number of moves is over 1 million
Summary >
So in this data structure tutorial we learned how to solve Towers of Hanoi problem with 3 disks in java. Program/Example of Towers of Hanoi problem with n disks in java. Solved Towers of Hanoi problem with 4 disks in java. Complexity of Towers of Hanoi problem with n disks in java.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
Program to Radix sort in java
Shell sorting in java
Labels:
Core Java
Level3 programs (advanced)