Tower of Hanoi in Java
In this post, you will learn the Tower of Hanoi program using the Java programming language.
The Tower of Hanoi is a mathematical puzzle containing 3 pillars or towers each with n disks of a different size/diameter.
Given a stack of n disks arranged from biggest on the bottom to smallest on top and put on a rod, along with two empty rods, the tower of Hanoi puzzle asks for the minimum number of moves required to move the stack from one rod to another, where moves are allowed only if they place smaller disks on the top of bigger disks.
Rules of Tower of Hanoi Problem
The rules for this puzzle are -
- We can move only one disk at a time.
- We can remove only the top disk.
- We can only place a disk above a disk of a bigger size.
Java Program for Tower of Hanoi Problem Using Recursion
A Recursion function is a function that is called by itself. In the given Java program, we call a recursion function towerOfHanoi() to get the Tower of Hanoi. Here, we have three towers named A, B, and C and asked the user to provide the number of plates and store it in a variable n. Then, we used the recursion function to solve it.
import java.util.Scanner;
public class TowerOfHanoiClass {
public static void main(String[] args) {
int n;
// Getting input from user
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of disks:");
n = sc.nextInt();
towerOfHanoi(n, 'A', 'B', 'C');
}
public static void towerOfHanoi(int topN, char source, char auxiliary, char destination) {
if (topN == 1) {
System.out.println("Disk 1 from " + source + " to " + destination);
} else {
towerOfHanoi(topN - 1, source, destination, auxiliary);
System.out.println("Disk " + topN + " from " + source + " to " + destination);
towerOfHanoi(topN - 1, auxiliary, source, destination);
}
}
}
Output of the above code:
Enter the number of disks:3
Disk 1 from A to C
Disk 2 from A to B
Disk 1 from C to B
Disk 3 from A to C
Disk 1 from B to A
Disk 2 from B to C
Disk 1 from A to C
Enter the number of disks:4
Disk 1 from A to B
Disk 2 from A to C
Disk 1 from B to C
Disk 3 from A to B
Disk 1 from C to A
Disk 2 from C to B
Disk 1 from A to B
Disk 4 from A to C
Disk 1 from B to C
Disk 2 from B to A
Disk 1 from C to A
Disk 3 from B to C
Disk 1 from A to B
Disk 2 from A to C
Disk 1 from B to C
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java