# Convert Integer to Roman Numerals in Java

### Problem statement:

Write a software/program/code to convert any integer to a roman number.

You might formulate this issue as either "Convert Decimal to Roman String" OR "Convert Number Value to Roman Number."

### Integer:

“IN-tuh-jer" is pronounced as Integer. An integer is a whole number which is a positive number or negative number, or zero, and an which is not a fraction.

Example: -2, -35,0,45,8567 and 764.

### Roman Numbers:

A unique type of number notation that was once employed by the Romans is known as Roman Numerals. Letters are used to represent specific base numbers and arbitrary numbers in the number system using the Roman numeral, which is an additive and subtractive system. Roman numeral XLVII, which in numerical form equals 47, is an illustration. Roman numerals are represented by a separate set of symbols, some of which are letters from the English alphabet.

Example:

### Some Rules are promoted to assign Roman numbers to an integer:

Roman numerals are typically written from left to right, highest to lowest, with a few exceptions when the left character is less than the right character. For instance, "IV" is comparable to 4 and not "IIII," If this applies, take the difference between the values of the left and right characters. IX will be 10-1 = 9, and IV will be 5-1 = 4. The cases are as follows:

Rule 1: I can come before V or X and stand for a one-percent subtraction, so IV (5-1) = 4 and IX (10-1) = 9, respectively.

Rule 2: X can come before L or C to stand for subtracting 10, making XL (50-10) equal to 40 and XC (100-10) equal to 90.

Rule 3: A C before a D or M stands for a hundred that has been subtracted, so CD (500-100) = 400 and CM (1000-100) = 900.

### Some of the examples are:

• The integer value of 25 with the Roman number is XXV
• The integer value of 36 with the Roman number is XXXVI
• The integer value of 1023 with the Roman number is MXXIII
• The integer value of 542 with the Roman number is DXLII

An algorithm or step-by-step sequence for this method:

• Step 1: Roman numeral letters and their related numerical values should be stored in an array.
• Step 2: Create a string builder and initialise it.
• Step 3: Next, check to see if the input number is greater than or equal to the highest roman numeral.
• Step 4: If so, add that number to the string builder and subtract the corresponding value from the input number.
• Step 5: If not, check with the next highest roman numeral. Repeat this process until the input number is 0.
• Step 6: Our romanization of the input number will be created by the string function Object () { [native code] }.

### Implementation code using Java

``````import java.util.*;
import java.io.*;
public class Romanconversion
{

public static void Romanconversion(int n)
{

System.out.println("Integer is: " + n);
int[] val = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
String[] romans = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};

StringBuilder rins = new StringBuilder();

for(int i=0;i<val.length;i++)
{
while(n >= val[i])
{
n-= val[i];
rins.append(romans[i]);
}
}
System.out.println("Roman numbers are: " + rins.toString());
System.out.println("---------------------------------------------------");
}

public static void main(String[] args)
{
Romanconversion(36);
Romanconversion(25);
Romanconversion(542);
Romanconversion(1023);
}
}
``````

Output:

``````cd /home/cg/root/63710dfb44386
Integer is: 36
Roman numbers are: XXXVI
---------------------------------------------------
Integer is: 25
Roman numbers are: XXV
---------------------------------------------------
Integer is: 542
Roman numbers are: DXLII
---------------------------------------------------
Integer is: 1023
Roman numbers are: MXXIII.
---------------------------------------------------
``````

The above example code is used for the conversion of roman numerals to the integer, which is of the special cases of highest and lowest terms.

• Here to get 25, we have to add five to the XX which is 20. To get 25 (twenty (XX)+five(V)), which is XXV.
• To get 36, we have to add one to five to get six, and we have to add that six to XXX, which is 30, so to get 36 thirty-six (thirty (XXX) + six (VI)), which is XXXVI.
• To get 542, we have to add two to fifty, and we have to subtract ten X from the fifty and we know that D means 500. So we will get (five hundred (D) +(fifty(LII)-ten(X))), which is DXLII.
• To get 1023 we must add three (III) to twenty (XX) and and we have to add it to a M that is thousand (thousand (M) +twenty (XX) + three(III)), which is MXXIII that is 1023.

### An Example for the conversion of integer to roman number for a particular set of values given as an input:

``````import java.util.*;
import java.io.*;
public class Romanconversion
{
//we have to write a procedural method for conversion of integer to the roman number
//create function for the conversion
public static String integerconversion(int n)
{
//now create the places of values to the each of them with their positions
String[] thou= {"", "M", "MM", "MMM"};
String[] hund = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
String[] ten = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
String[] ones = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
return thou[n/ 1000] + hund[(n % 1000) / 100] + ten[(n % 100) / 10] + ones[n % 10];
}
public static void main(String args[])
{
//now make or create an array that consist of integers used for the conversion from integer to roman numbers
int[] nums = {10,12,13,19,43,21,29,38,47,50,76,190, 141, 117, 120,1897,125, 138, 149, 6, 712, 181, 197, 918, 199, 1100, 1101, 1248,1234,1253};
for(int n : nums)
{
System.out.printf("%4d -> %8s\n", n,integerconversion(n));
}
}
}
``````

Output:

``````cd /home/cg/root/6372446633d0f
10 -> X
12 -> XII
13 -> XIII
19->      XIX
43->    XLIII
21->      XXI
29->     XXIX
38->  XXXVIII
47->    XLVII
50->        L
76->    LXXVI
190->      CXC
141->     CXLI
117->    CXVII
120->      CXX
1897-> MDCCCXCVII
125->     CXXV
138-> CXXXVIII
149->    CXLIX
6->       VI
712->   DCCXII
181->   CLXXXI
197->   CXCVII
918->  CMXVIII
199->    CXCIX
1100->       MC
1101->      MCI
1248-> MCCXLVIII
1234-> MCCXXXIV
1253->  MCCLIII
``````

The above code gives the roman characters to the particular set of integers.

Now we are going to write code for the conversion of integers to the roman numbers whereas here we are going to give some range so upto that particular range the output will be printed.

### Example code for the conversion within the range

``````import java.util.LinkedHashMap;
import java.util.*;
import java.io.*;
import java.util.Map;
public class Romanconversion
{
public static String RomanNumbers(int n)
{
//we have to store the decimal values with respect to the hashmap
roman.put("M", 1000);
roman.put("CM", 900);
roman.put("D", 500);
roman.put("CD", 400);
roman.put("C", 100);
roman.put("XC", 90);
roman.put("L", 50);
roman.put("XL", 40);
roman.put("X", 10);
roman.put("IX", 9);
roman.put("V", 5);
roman.put("IV", 4);
roman.put("I", 1);
//we have to take a string for the result
String res = "";
//take a for loop and iterate through the map
for(Map.Entry<String, Integer> keep : roman.entrySet())
{
int match = n/keep.getValue();
res = res+iterate(keep.getKey(), match);
n  = n % keep.getValue();
}
return res;
}
public static String iterate(String str, int num)
{
if(str == null)
{
return null;
}
final StringBuilder sbli = new StringBuilder();
for(int i = 0; i < num; i++)
{
sbli.append(str);
}
//conversion to the string
return sbli.toString();
}
public static void main(String args[])
{
//now print the roman numbers from 1 to 300
for (int i = 1;i<301;i++)
{
System.out.println("i="+i+" -> "+RomanNumbers(i));
}
}
}
``````

Output:

``````cd /home/cg/root/63724af1a7770
i=1 -> I
i=2 -> II
i=3 -> III
i=4 -> IV
i=5 -> V
i=6 -> VIi=7 -> VIIi=8 -> VIII
i=9 -> IX
i=10 -> X
i=11 -> XIi=12 -> XIIi=13 -> XIII
i=14 -> XIV
i=15 -> XVi=16 -> XVI
i=17 -> XVIIi=18 -> XVIII
i=19 -> XIX
i=20 -> XX
i=21 -> XXIi=22 -> XXII
i=23 -> XXIII
i=24 -> XXIV
i=25 -> XXV
i=26 -> XXVI
i=27 -> XXVIIi=28 -> XXVIII
i=29 -> XXIX
i=30 -> XXXi=31 -> XXXIi=32 -> XXXII
i=33 -> XXXIII
i=34 -> XXXIVi=35 -> XXXV
i=36 -> XXXVI
i=37 -> XXXVII
i=38 -> XXXVIII
i=39 -> XXXIXi=40 -> XL
i=41 -> XLIi=42 -> XLII
i=43 -> XLIII
i=44 -> XLIV
i=45 -> XLV
i=46 -> XLVI
i=47 -> XLVII
i=48 -> XLVIII
i=49 -> XLIX
i=50 -> Li=51 -> LI
i=52 -> LII
i=53 -> LIII
i=54 -> LIV
i=55 -> LV
i=56 -> LVI
i=57 -> LVII
i=58 -> LVIII
i=59 -> LIX
i=60 -> LX
i=61 -> LXI
i=62 -> LXIIi=63 -> LXIII
i=64 -> LXIVi=65 -> LXV
i=66 -> LXVI
i=67 -> LXVII
i=68 -> LXVIII
i=69 -> LXIX
i=70 -> LXX
i=71 -> LXXI
i=72 -> LXXII
i=73 -> LXXIII
i=74 -> LXXIV
i=75 -> LXXV
i=76 -> LXXVI
i=77 -> LXXVII
i=78 -> LXXVIII
i=79 -> LXXIX
i=80 -> LXXX
i=81 -> LXXXI
i=82 -> LXXXII
i=83 -> LXXXIII
i=84 -> LXXXIV
i=85 -> LXXXV
i=86 -> LXXXVI
i=87 -> LXXXVIIi=88 -> LXXXVIIIi=89 -> LXXXIXi=90 -> XCi=91 -> XCI
i=92 -> XCII
i=93 -> XCIII
i=94 -> XCIV
i=95 -> XCVi=96 -> XCVI
i=97 -> XCVII
i=98 -> XCVIII
i=99 -> XCIX
i=100 -> C
i=101 -> CI
i=102 -> CII
i=103 -> CIII
i=104 -> CIV
i=105 -> CV
i=106 -> CVI
i=107 -> CVII
i=108 -> CVIII
i=109 -> CIX
i=110 -> CX
i=111 -> CXI
i=112 -> CXII
i=113 -> CXIII
i=114 -> CXIV
i=115 -> CXV
i=116 -> CXVI
i=117 -> CXVII
i=118 -> CXVIII
i=119 -> CXIX
i=120 -> CXX
i=121 -> CXXI
i=122 -> CXXII
i=123 -> CXXIII
i=124 -> CXXIV
i=125 -> CXXV
i=126 -> CXXVI
i=127 -> CXXVII
i=128 -> CXXVIII
i=129 -> CXXIX
i=130 -> CXXX
i=131 -> CXXXI
i=132 -> CXXXII
i=133 -> CXXXIII
i=134 -> CXXXIV
i=135 -> CXXXV
i=136 -> CXXXVI
i=137 -> CXXXVII
i=138 -> CXXXVIIIi=139 -> CXXXIXi=140 -> CXLi=141 -> CXLIi=142 -> CXLII
i=143 -> CXLIII
i=144 -> CXLIV
i=145 -> CXLV
i=146 -> CXLVI
i=147 -> CXLVII
i=148 -> CXLVIIIi=149 -> CXLIXi=150 -> CLi=151 -> CLIi=152 -> CLIIi=153 -> CLIIIi=154 -> CLIV
i=155 -> CLV
i=156 -> CLVIi=157 -> CLVII
i=158 -> CLVIII
i=159 -> CLIX
i=160 -> CLX
i=161 -> CLXI
i=162 -> CLXII
i=163 -> CLXIII
i=164 -> CLXIV
i=165 -> CLXV
i=166 -> CLXVI
i=167 -> CLXVII
i=168 -> CLXVIII
i=169 -> CLXIX
i=170 -> CLXX
i=171 -> CLXXI
i=172 -> CLXXII
i=173 -> CLXXIII
i=174 -> CLXXIV
i=175 -> CLXXVi=176 -> CLXXVI
i=177 -> CLXXVII
i=178 -> CLXXVIII
i=179 -> CLXXIX
i=180 -> CLXXX
i=181 -> CLXXXI
i=182 -> CLXXXIIi=183 -> CLXXXIIIi=184 -> CLXXXIV
i=185 -> CLXXXV
i=186 -> CLXXXVI
i=187 -> CLXXXVIIi=188 -> CLXXXVIII
i=189 -> CLXXXIX
i=190 -> CXC
i=191 -> CXCI
i=192 -> CXCIIi=193 -> CXCIII
i=194 -> CXCIV
i=195 -> CXCV
i=196 -> CXCVIi=197 -> CXCVII
i=198 -> CXCVIII
i=199 -> CXCIX
i=200 -> CC
i=201 -> CCI
i=202 -> CCII
i=203 -> CCIII
i=204 -> CCIVi=205 -> CCV
i=206 -> CCVI
i=207 -> CCVII
i=208 -> CCVIII
i=209 -> CCIX
i=210 -> CCXi=211 -> CCXI
i=212 -> CCXII
i=213 -> CCXIII
i=214 -> CCXIV
i=215 -> CCXV
i=216 -> CCXVIi=217 -> CCXVII
i=218 -> CCXVIII
i=219 -> CCXIX
i=220 -> CCXX
i=221 -> CCXXI
i=222 -> CCXXIIi=223 -> CCXXIIIi=224 -> CCXXIV
i=225 -> CCXXV
i=226 -> CCXXVI
i=227 -> CCXXVII
i=228 -> CCXXVIII
i=229 -> CCXXIXi=230 -> CCXXX
i=231 -> CCXXXI
i=232 -> CCXXXIIi=233 -> CCXXXIII
i=234 -> CCXXXIV
i=235 -> CCXXXV
i=236 -> CCXXXVI
i=237 -> CCXXXVII
i=238 -> CCXXXVIII
i=239 -> CCXXXIXi=240 -> CCXL
i=241 -> CCXLI
i=242 -> CCXLIIi=243 -> CCXLIII
i=244 -> CCXLIV
i=245 -> CCXLV
i=246 -> CCXLVI
i=247 -> CCXLVII
i=248 -> CCXLVIII
i=249 -> CCXLIX
i=250 -> CCL
i=251 -> CCLI
i=252 -> CCLII
i=253 -> CCLIIIi=254 -> CCLIV
i=255 -> CCLV
i=256 -> CCLVIi=257 -> CCLVII
i=258 -> CCLVIIIi=259 -> CCLIX
i=260 -> CCLX
i=261 -> CCLXI
i=262 -> CCLXII
i=263 -> CCLXIII
i=264 -> CCLXIV
i=265 -> CCLXV
i=266 -> CCLXVI
i=267 -> CCLXVII
i=268 -> CCLXVIII
i=269 -> CCLXIX
i=270 -> CCLXX
i=271 -> CCLXXI
i=272 -> CCLXXII
i=273 -> CCLXXIII
i=274 -> CCLXXIV
i=275 -> CCLXXV
i=276 -> CCLXXVI
i=277 -> CCLXXVII
i=278 -> CCLXXVIII
i=279 -> CCLXXIX
i=280 -> CCLXXX
i=281 -> CCLXXXI
i=282 -> CCLXXXII
i=283 -> CCLXXXIII
i=284 -> CCLXXXIV
i=285 -> CCLXXXV
i=286 -> CCLXXXVI
i=287 -> CCLXXXVII
i=288 -> CCLXXXVIII
i=289 -> CCLXXXIX
i=290 -> CCXC
i=291 -> CCXCI
i=292 -> CCXCII
i=293 -> CCXCIII
i=294 -> CCXCIV
i=295 -> CCXCV
i=296 -> CCXCVI
i=297 -> CCXCVII
i=298 -> CCXCVIIIi=299 -> CCXCIX
i=300 -> CCC
``````