Java Program to Convert Numbers to Roman Numerals and Vice Versa
This program includes two main functionalities:
- Converting an integer to a Roman numeral
- Converting a Roman numeral to an integer
Program Structure
The program consists of a single class RomanNumeralConverter
which contains two static methods:
intToRoman(int num)
: Converts an integer to a Roman numeralromanToInt(String s)
: Converts a Roman numeral to an integer
Java Code
import java.util.HashMap;
import java.util.Map;
public class RomanNumeralConverter {
// Array of Roman numeral symbols and their corresponding integer values
private static final int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
private static final String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
/**
* Converts an integer to a Roman numeral.
*
* @param num the integer to be converted
* @return the Roman numeral representation of the integer
*/
public static String intToRoman(int num) {
StringBuilder roman = new StringBuilder();
for (int i = 0; i < values.length; i++) { while (num >= values[i]) {
num -= values[i];
roman.append(symbols[i]);
}
}
return roman.toString();
}
// Map of Roman numeral symbols and their corresponding integer values
private static final Map<Character, Integer> romanToIntMap = new HashMap<>();
static {
romanToIntMap.put('I', 1);
romanToIntMap.put('V', 5);
romanToIntMap.put('X', 10);
romanToIntMap.put('L', 50);
romanToIntMap.put('C', 100);
romanToIntMap.put('D', 500);
romanToIntMap.put('M', 1000);
}
/**
* Converts a Roman numeral to an integer.
*
* @param s the Roman numeral to be converted
* @return the integer representation of the Roman numeral
*/
public static int romanToInt(String s) {
int sum = 0;
for (int i = 0; i < s.length(); i++) {
int currentVal = romanToIntMap.get(s.charAt(i));
int nextVal = (i + 1 < s.length()) ? romanToIntMap.get(s.charAt(i + 1)) : 0;
if (currentVal < nextVal) { sum -= currentVal; } else { sum += currentVal; } } return sum; } // Main method to test the conversion methods public static void main(String[] args) { // Test int to Roman conversion int number = 1987; String romanNumeral = intToRoman(number); System.out.println("Integer: " + number + " -> Roman Numeral: " + romanNumeral);
// Test Roman to int conversion
String roman = "MCMLXXXVII";
int integer = romanToInt(roman);
System.out.println("Roman Numeral: " + roman + " -> Integer: " + integer);
}
}
Explanation
The program first defines two arrays, values
and symbols
, which hold the integer values and corresponding Roman numeral symbols respectively. The intToRoman
method iterates over these arrays, subtracting the integer values from the input number and appending the corresponding symbols to a StringBuilder
to build the Roman numeral.
The romanToInt
method uses a HashMap
to map Roman numeral characters to their integer values. It then iterates over the input string, checking the value of each character and the character immediately following it. If a character has a lesser value than the following character, its value is subtracted from the sum; otherwise, it is added to the sum.