Skip to content

Commit 60ab08d

Browse files
SpreehaMadhavBahl
authored andcommitted
day 16 (#159)
1 parent 2740368 commit 60ab08d

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

day16/Java/towersOfHanoi.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}

day16/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,44 @@ console.log ('\n/* ===== for 3 disks ===== */');
4646
towerOfHanoi (3, 'A', 'C', 'B');
4747
```
4848

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+
4987
## Ruby Implementation
5088

5189
### [Solution](./Ruby/hanoi.rb)

0 commit comments

Comments
 (0)