Skip to content

Commit d802790

Browse files
committed
Integer To Roman
1 parent 990b88f commit d802790

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Conversions/IntegerToRoman.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package Conversions;
2+
3+
public class IntegerToRoman {
4+
private static int[] allArabianRomanNumbers = new int[] {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
5+
private static String[] allRomanNumbers = new String[] {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
6+
7+
public static String integerToRoman(int num) {
8+
if(num <= 0) {
9+
return "";
10+
}
11+
12+
StringBuilder builder = new StringBuilder();
13+
14+
for(int a = 0;a < allArabianRomanNumbers.length;a++) {
15+
int times = num / allArabianRomanNumbers[a];
16+
for(int b = 0;b < times;b++) {
17+
builder.append(allRomanNumbers[a]);
18+
}
19+
20+
num -= times * allArabianRomanNumbers[a];
21+
}
22+
23+
return builder.toString();
24+
}
25+
26+
public static void main(String[] args) {
27+
System.out.println(IntegerToRoman.integerToRoman(2131));
28+
}
29+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy