-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTower.java
More file actions
33 lines (31 loc) · 787 Bytes
/
Tower.java
File metadata and controls
33 lines (31 loc) · 787 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
31
32
33
import java.util.Scanner;
public class Tower
{
public static void main(String[] args)
{
Scanner scan =new Scanner(System.in);
int num=scan.nextInt();
System.out.println("Enter from tower , intermediate tower and to tower");
char a=scan.next().charAt(0);
char b=scan.next().charAt(0);
char c=scan.next().charAt(0);
scan.close();
System.out.println("Hanoi tower series is ");
Hanoi h=new Hanoi();
h.hanoi(num,a,b,c);
}
}
class Hanoi
{
public void hanoi(int num,char first,char intermediate,char last)
{
if(num==1)
{
System.out.println("Move disc 1 from "+first+" to "+last);
return;
}
hanoi(num-1,first,last,intermediate);
System.out.printf("Move disk %d from tower %c to tower %c\n",num,first,last);
hanoi(num-1,intermediate,first,last);
}
}