-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay13.java
More file actions
30 lines (30 loc) · 980 Bytes
/
Copy pathDay13.java
File metadata and controls
30 lines (30 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.*;
class Toh{
int steps = 1;
void towerOfHanoi(int n,char start,char spare,char dest){
if (n==1) {
System.out.println("Step:"+ steps++ +":Move "+n+" disk from " +start+ " to "+dest);// direct start->dest
}
else{
towerOfHanoi(n-1,start,dest,spare);//first to spare
System.out.println("Step:"+ steps++ +":Move "+n+" disk from " +start+ " to "+dest);
towerOfHanoi(n-1,spare,start,dest);//second from spare to dest
}
}
}
public class Day13{
public static void main(String args[])
{
Scanner in =new Scanner(System.in);
System.out.println("Enter the number of Disks");
int n = in.nextInt();
Toh t = new Toh();
System.out.println("Assign a Character to your Source");
char a = in.next().charAt(0);
System.out.println("Assign a Character to your Temporary");
char b = in.next().charAt(0);
System.out.println("Assign a Character to your Destination");
char c = in.next().charAt(0);
t.towerOfHanoi(n, a, b, c);
}
}