Skip to content

Commit 2ca81bd

Browse files
Create Towerofhanoi.java
1 parent a9ea7b3 commit 2ca81bd

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Towerofhanoi.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.JournalDev;
2+
public class Main {
3+
static void towerOfHanoi(int n, char from_rod, char to_rod, char helper_rod)
4+
{
5+
if (n == 1)
6+
{
7+
System.out.println("Take disk 1 from rod " + from_rod + " to rod " + to_rod);
8+
return;
9+
}
10+
towerOfHanoi(n-1, from_rod, helper_rod, to_rod);
11+
System.out.println("Take disk " + n + " from rod " + from_rod + " to rod " + to_rod);
12+
towerOfHanoi(n-1, helper_rod, to_rod, from_rod);
13+
}
14+
15+
public static void main(String args[])
16+
{
17+
int n = 5;
18+
towerOfHanoi(n,'A','C', 'B');
19+
}
20+
21+
}

0 commit comments

Comments
 (0)