diff --git a/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java b/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java
index 9c031df95..fec437668 100644
--- a/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java
+++ b/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java
@@ -1,68 +1,68 @@
package com.thealgorithms.conversions;
/**
- * Converting Integers into Roman Numerals
+ * A utility class to convert integers into Roman numerals.
*
- *
- * ('I', 1); ('IV',4); ('V', 5); ('IX',9); ('X', 10); ('XL',40); ('L', 50);
- * ('XC',90); ('C', 100); ('D', 500); ('M', 1000);
+ *
Roman numerals follow these rules:
+ *
+ * - I = 1
+ * - IV = 4
+ * - V = 5
+ * - IX = 9
+ * - X = 10
+ * - XL = 40
+ * - L = 50
+ * - XC = 90
+ * - C = 100
+ * - D = 500
+ * - M = 1000
+ *
+ *
+ * Conversion is based on repeatedly subtracting the largest possible Roman numeral value
+ * from the input number until it reaches zero. For example, 1994 is converted as:
+ *
+ * 1994 -> MCMXCIV (1000 + 900 + 90 + 4)
+ *
*/
public final class IntegerToRoman {
+
+ // Array of Roman numeral values in descending order
+ private static final int[] ALL_ROMAN_NUMBERS_IN_ARABIC = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
+
+ // Corresponding Roman numeral symbols
+ private static final String[] ALL_ROMAN_NUMBERS = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
+
private IntegerToRoman() {
}
- private static final int[] ALL_ROMAN_NUMBERS_IN_ARABIC = new int[] {
- 1000,
- 900,
- 500,
- 400,
- 100,
- 90,
- 50,
- 40,
- 10,
- 9,
- 5,
- 4,
- 1,
- };
- private static final String[] ALL_ROMAN_NUMBERS = new String[] {
- "M",
- "CM",
- "D",
- "CD",
- "C",
- "XC",
- "L",
- "XL",
- "X",
- "IX",
- "V",
- "IV",
- "I",
- };
-
- // Value must be > 0
+ /**
+ * Converts an integer to its Roman numeral representation.
+ * Steps:
+ *
+ * - Iterate over the Roman numeral values in descending order
+ * - Calculate how many times a numeral fits
+ * - Append the corresponding symbol
+ * - Subtract the value from the number
+ * - Repeat until the number is zero
+ * - Return the Roman numeral representation
+ *
+ *
+ * @param num the integer value to convert (must be greater than 0)
+ * @return the Roman numeral representation of the input integer
+ * or an empty string if the input is non-positive
+ */
public static String integerToRoman(int num) {
if (num <= 0) {
return "";
}
StringBuilder builder = new StringBuilder();
-
- for (int a = 0; a < ALL_ROMAN_NUMBERS_IN_ARABIC.length; a++) {
- int times = num / ALL_ROMAN_NUMBERS_IN_ARABIC[a];
- for (int b = 0; b < times; b++) {
- builder.append(ALL_ROMAN_NUMBERS[a]);
- }
-
- num -= times * ALL_ROMAN_NUMBERS_IN_ARABIC[a];
+ for (int i = 0; i < ALL_ROMAN_NUMBERS_IN_ARABIC.length; i++) {
+ int times = num / ALL_ROMAN_NUMBERS_IN_ARABIC[i];
+ builder.append(ALL_ROMAN_NUMBERS[i].repeat(Math.max(0, times)));
+ num -= times * ALL_ROMAN_NUMBERS_IN_ARABIC[i];
}
return builder.toString();
}
-
- public static void main(String[] args) {
- System.out.println(IntegerToRoman.integerToRoman(2131));
- }
}
diff --git a/src/test/java/com/thealgorithms/conversions/IntegerToRomanTest.java b/src/test/java/com/thealgorithms/conversions/IntegerToRomanTest.java
index 04768d034..181cad930 100644
--- a/src/test/java/com/thealgorithms/conversions/IntegerToRomanTest.java
+++ b/src/test/java/com/thealgorithms/conversions/IntegerToRomanTest.java
@@ -10,5 +10,30 @@ public class IntegerToRomanTest {
public void testIntegerToRoman() {
assertEquals("MCMXCIV", IntegerToRoman.integerToRoman(1994));
assertEquals("LVIII", IntegerToRoman.integerToRoman(58));
+ assertEquals("IV", IntegerToRoman.integerToRoman(4));
+ assertEquals("IX", IntegerToRoman.integerToRoman(9));
+ assertEquals("MMM", IntegerToRoman.integerToRoman(3000));
+ }
+
+ @Test
+ public void testSmallNumbers() {
+ assertEquals("I", IntegerToRoman.integerToRoman(1));
+ assertEquals("II", IntegerToRoman.integerToRoman(2));
+ assertEquals("III", IntegerToRoman.integerToRoman(3));
+ }
+
+ @Test
+ public void testRoundNumbers() {
+ assertEquals("X", IntegerToRoman.integerToRoman(10));
+ assertEquals("L", IntegerToRoman.integerToRoman(50));
+ assertEquals("C", IntegerToRoman.integerToRoman(100));
+ assertEquals("D", IntegerToRoman.integerToRoman(500));
+ assertEquals("M", IntegerToRoman.integerToRoman(1000));
+ }
+
+ @Test
+ public void testEdgeCases() {
+ assertEquals("", IntegerToRoman.integerToRoman(0)); // Non-positive number case
+ assertEquals("", IntegerToRoman.integerToRoman(-5)); // Negative number case
}
}