-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoman Numeral
23 lines (23 loc) · 1.01 KB
/
Roman Numeral
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.*;
public class RomanNumeral
{
public static void main(String[] args){
System.out.println("This program will take a number and output it as a Roman Numeral. ");
System.out.println("\nPlease enter a number between 1 and 3999: ");
Scanner input = new Scanner(System.in);
long number = input.nextLong();
int i = 0;
String romanNum = "";
if (number < 1 || number > 3999){
System.out.println("\nInvalid Input");
}
else{
while(i < number){
romanNum = romanNum + "I";
i++;
}
romanNum = romanNum.replaceAll("IIIII","V").replaceAll("IIII","IV").replaceAll("VV","X").replaceAll("VIV","IX").replaceAll("XXXXX","L").replaceAll("XXXX","XL").replaceAll("LL","C").replaceAll("LXL","XC").replaceAll("CCCCC","D").replaceAll("CCCC","CD").replaceAll("DD","M").replaceAll("DCD","CM");
System.out.println(number+" in Roman Numerals is " + romanNum);
}
}
}