Skip to content

Commit 7ed38b2

Browse files
Nikhilesh SatishNikhilesh Satish
authored andcommitted
Added the recursive solution for tower of hanoi
1 parent b316dcf commit 7ed38b2

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.thealgorithms.recursion;
2+
3+
public class TowerOfHanoi {
4+
public void solveHanoi(int n,char src,char dest,char aux)
5+
{
6+
if(n==1)
7+
{
8+
System.out.println("Move disk 1 from "+src+" to "+dest);
9+
return;
10+
}
11+
solveHanoi(n-1,src,aux,dest);
12+
System.out.println("Move disk "+n+" from "+src+" to "+dest);
13+
solveHanoi(n-1,aux,dest,src);
14+
}
15+
}

0 commit comments

Comments
 (0)