FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaProgramming/Recursion/Hanoi.java at main · PrityKumari14/JavaProgramming · GitHub
PrityKumari14
/
JavaProgramming
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaProgramming
/
Recursion
/
Hanoi.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
31 lines (30 loc) · 1 KB
Breadcrumbs
JavaProgramming
/
Recursion
/
Hanoi.java
Copy path
File metadata and controls
31 lines (30 loc) · 1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
Recursion
;
import
java
.
util
.*;
public
class
Hanoi
{
public
static
void
towerOfHanoi
(
int
n
,
String
src
,
String
helper
,
String
dest
) {
if
(
n
==
1
) {
System
.
out
.
println
(
"transfer disk "
+
n
+
" from "
+
src
+
" to "
+
dest
);
return
;
}
//transfer top n-1 from src to helper using dest as 'helper'
towerOfHanoi
(
n
-
1
,
src
,
dest
,
helper
);
//transfer nth from src to dest
System
.
out
.
println
(
"transfer disk "
+
n
+
" from "
+
src
+
" to "
+
helper
);
//transfer n-1 from helper to dest using src as 'helper'
towerOfHanoi
(
n
-
1
,
helper
,
src
,
dest
);
}
public
static
void
main
(
String
args
[]) {
int
n
=
3
;
towerOfHanoi
(
n
,
"S"
,
"H"
,
"D"
);
// S=source, D=Destination, H=helper
}
}
//output
/*
transfer disk 1 from S to D
transfer disk 2 from S to D
transfer disk 1 from D to H
transfer disk 3 from S to H
transfer disk 1 from H to S
transfer disk 2 from H to S
transfer disk 1 from S to D
*/
Back
|
FazBrowse Home
|
New Git URL