Skip to content

Commit 58b78e0

Browse files
authored
Merge pull request #181 from PiyushPawar17/PiyushPawar17_tower_of_hanoi_in_java
Added Tower of Hanoi in Java
2 parents 4a5729c + f29a40e commit 58b78e0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

recursion/tower_of_hanoi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class tower_of_hanoi {
2+
public static void TowerOfHanoi(int top, char start, char via, char end) {
3+
if (top == 1)
4+
System.out.println(start + " --> " + end);
5+
else {
6+
TowerOfHanoi(top - 1, start, end, via);
7+
System.out.println(start + " --> " + end);
8+
TowerOfHanoi(top - 1, via, start, end);
9+
}
10+
}
11+
12+
public static void main(String[] args) {
13+
int blocks = 5; // Number of blocks in the tower
14+
TowerOfHanoi(blocks, 'A', 'B', 'C'); // Moving all blocks from A to C
15+
}
16+
}

0 commit comments

Comments
 (0)