-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtowerofhonni.java
More file actions
26 lines (21 loc) · 1019 Bytes
/
towerofhonni.java
File metadata and controls
26 lines (21 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package daaa;
public class TowerOfHanoi {
// Recursive function to solve Tower of Hanoi puzzle
public static void solveHanoi(int n, char source, char auxiliary, char destination) {
if (n == 1) {
System.out.println("Move disk 1 from " + source + " to " + destination);
return;
}
// Move n-1 disks from source to auxiliary using destination as buffer
solveHanoi(n - 1, source, destination, auxiliary);
// Move the nth disk from source to destination
System.out.println("Move disk " + n + " from " + source + " to " + destination);
// Move n-1 disks from auxiliary to destination using source as buffer
solveHanoi(n - 1, auxiliary, source, destination);
}
public static void main(String[] args) {
int numberOfDisks = 5; // You can change this number to solve for more disks
System.out.println("Solution for " + numberOfDisks + " disks:");
solveHanoi(numberOfDisks, 'A', 'B', 'C');
}
}