File tree 2 files changed +74
-0
lines changed
2 files changed +74
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+ package recursion ;
7
+
8
+ /**
9
+ * @date 10/01/19
10
+ * @author SPREEHA DUTTA
11
+ */
12
+ import java .util .*;
13
+ public class towersOfHanoi {
14
+ public static void move (int n ,char s ,char d ,char c )
15
+ {
16
+ if (n ==1 )
17
+ {
18
+ System .out .println ("Move disk " +n +" from " +s +" to " +d );
19
+ return ;
20
+ }
21
+ else
22
+ {
23
+ move (n -1 ,s ,c ,d );
24
+ System .out .println ("Move disk " +n +" from " +s +" to " +d );
25
+ move (n -1 ,c ,d ,s );
26
+ }
27
+ }
28
+ public static void main (String []args )
29
+ {
30
+ Scanner sc =new Scanner (System .in );
31
+ int n ; char start ='S' ;char center ='C' ; char destination ='D' ;
32
+ System .out .println ("Enter number of disks" );
33
+ n =sc .nextInt ();
34
+ move (n ,start ,destination ,center );
35
+ }
36
+ }
Original file line number Diff line number Diff line change @@ -46,6 +46,44 @@ console.log ('\n/* ===== for 3 disks ===== */');
46
46
towerOfHanoi (3 , ' A' , ' C' , ' B' );
47
47
```
48
48
49
+ ## Java Implementation
50
+
51
+ ### [ Solution] ( ./Java/towersOfHanoi.java )
52
+
53
+ ``` java
54
+ /**
55
+ * @date 10/01/19
56
+ * @author SPREEHA DUTTA
57
+ */
58
+ import java.util.* ;
59
+ public class towersOfHanoi
60
+ {
61
+ public static void move (int n ,char s ,char d ,char c )
62
+ {
63
+ if (n== 1 )
64
+ {
65
+ System . out. println(" Move disk " + n+ " from " + s+ " to " + d);
66
+ return ;
67
+ }
68
+ else
69
+ {
70
+ move(n- 1 ,s,c,d);
71
+ System . out. println(" Move disk " + n+ " from " + s+ " to " + d);
72
+ move(n- 1 ,c,d,s);
73
+ }
74
+ }
75
+ public static void main (String []args )
76
+ {
77
+ Scanner sc= new Scanner (System . in);
78
+ int n; char start= ' S' ;char center= ' C' ; char destination= ' D' ;
79
+ System . out. println(" Enter number of disks" );
80
+ n= sc. nextInt();
81
+ move(n,start,destination,center);
82
+ }
83
+ }
84
+ }
85
+ ```
86
+
49
87
## Ruby Implementation
50
88
51
89
### [ Solution] ( ./Ruby/hanoi.rb )
You can’t perform that action at this time.
0 commit comments