Skip to content

Commit 41b5b48

Browse files
Hard Level
The objective of this puzzle is to move the discs, one at a time, from start to finish but you are not allowed to put a disc on top of a smaller disc.
1 parent 69a9687 commit 41b5b48

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Tower_of_Hanoi.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Class
2+
public class Tower_of_Hanoi {
3+
4+
//Function
5+
public static void toh(int disk, char src, char hel, char des) {
6+
if(disk == 0) {
7+
return;
8+
}
9+
10+
//Recursion 1
11+
toh(disk-1, src, des, hel);
12+
13+
//Print Result
14+
System.out.println("Disk "+disk+" "+src+" "+des);
15+
16+
//Recursion 2
17+
toh(disk-1, hel, src, des);
18+
}
19+
public static void main(String args[]) {
20+
toh(3, 'S', 'H', 'D');
21+
}
22+
}
23+
24+
//OUTPUT :
25+
// Disk 1 S D
26+
// Disk 2 S H
27+
// Disk 1 D H
28+
// Disk 3 S D
29+
// Disk 1 H S
30+
// Disk 2 H D
31+
// Disk 1 S D

0 commit comments

Comments
 (0)