Skip to content

Commit 3555538

Browse files
soln for tower of hanoi problem
1 parent 3d0d03a commit 3555538

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

tower_of_hanoi.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//plss accept my pr, this cpde is for solving tower of hanoi problem
2+
3+
class Main {
4+
5+
public static void main(String[] args) {
6+
hanoi(3, 'A', 'B', 'C');
7+
}
8+
9+
private static void hanoi(int n, char rodFrom, char rodMiddle, char rodTo){
10+
11+
if(n==1){
12+
System.out.println("Disk 1 moved from "+rodFrom+" to "+rodTo);
13+
return;
14+
}
15+
16+
//Move top n-1 disks from A to B using C as middle
17+
hanoi(n-1,rodFrom,rodTo,rodMiddle);
18+
19+
//Move last disk from A to C
20+
System.out.println("Disk "+n+" moved from "+rodFrom+" to "+rodTo);
21+
22+
//Move n-1 disks from B to C using A as middle
23+
hanoi(n-1,rodMiddle,rodFrom,rodTo);
24+
25+
}
26+
}

0 commit comments

Comments
 (0)