FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Conversions/IntegerToRoman.java at master · loisoft/Java · GitHub
loisoft
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Conversions
/
IntegerToRoman.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (31 loc) · 1.04 KB
Breadcrumbs
Java
/
Conversions
/
IntegerToRoman.java
Copy path
File metadata and controls
39 lines (31 loc) · 1.04 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
32
33
34
35
36
37
38
39
package
Conversions
;
/**
* Converting Integers into Roman Numerals
*
* <p>('I', 1); ('IV',4); ('V', 5); ('IV',9); ('X', 10); ('XL',40; ('L', 50); ('XC',90); ('C', 100);
* ('D', 500); ('M', 1000);
*/
public
class
IntegerToRoman
{
private
static
int
[]
allArabianRomanNumbers
=
new
int
[] {
1000
,
900
,
500
,
400
,
100
,
90
,
50
,
40
,
10
,
9
,
5
,
4
,
1
};
private
static
String
[]
allRomanNumbers
=
new
String
[] {
"M"
,
"CM"
,
"D"
,
"CD"
,
"C"
,
"XC"
,
"L"
,
"XL"
,
"X"
,
"IX"
,
"V"
,
"IV"
,
"I"
};
// Value must be > 0
public
static
String
integerToRoman
(
int
num
) {
if
(
num
<=
0
) {
return
""
;
}
StringBuilder
builder
=
new
StringBuilder
();
for
(
int
a
=
0
;
a
<
allArabianRomanNumbers
.
length
;
a
++) {
int
times
=
num
/
allArabianRomanNumbers
[
a
];
for
(
int
b
=
0
;
b
<
times
;
b
++) {
builder
.
append
(
allRomanNumbers
[
a
]);
}
num
-=
times
*
allArabianRomanNumbers
[
a
];
}
return
builder
.
toString
();
}
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
IntegerToRoman
.
integerToRoman
(
2131
));
}
}
Back
|
FazBrowse Home
|
New Git URL