This code is used to convert the highest possible number into Roman string in Java i.e 1000=M, 500=D, 100=C, 50=L,10=X, 5=V, 1=I

In this code I have used 7 different letters i.e **I**, **V**, **X**, **L**, **C**, **D**, **M** to write Roman Numerals and they represent the number 1,5,10,50,100,500,1000. 7 different letters are used to make up thousands of others.

for example, 3 is written as **III** which is just three **I**'s smushed together.

The trick to writing a Roman numeral is to write it from left to right i.e from highest to lowest. In some extreme cases where the left character is less the right character, for example, **IV **is written as 4, not **IIII, **In these cases, we subtract the left character from the right character.

for example:

** C** placed before **D** or **M** represents **subtract hundred**, so **CM** (1000-100)=900.

** X placed before L or C represents subtract ten, so XC (100-10)=90.**

** I** placed before **V** or **X, **represents** subtract** **one**, so 9 is **IX** (10-1)=9.

** ****I have initialized a string = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] **

**String[] Roman = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]**

Input number greater than highest roman numeral is added to the string and its value is reduced from previous input number, the input number less than highest roman numeral is compared with next highest Roman numeral and the process is repeated till input number becomes 0.

**Approach:**

N=26

1000>26, check with next roman number, result =""

900>26, check with next roman numeral, result=""

500>26, check with next roman numeral, result=""

400>26, check with next roman numeral, result=""

100>26, check with next roman numeral, result=""

90>26, check with next roman numeral, result=""

50>26, check with next roman numeral, result=""

40>26, check with next roman numeral, result=""

10<26, add it to string, result = **X ,26-10 =16**

10<16, add it to string, result = **XX ,16-10 =6**

10>6 , check with next roman numeral, result="**XX**"

9>6 , check with next roman numeral, result**="XX"**

5<6, add corresponding literal 'V' to result , result =XXV, N = 6-5=1

5>1, check with next roman numeral, result =XXV

4>1, check with next roman numeral, result =XXV

1==1, add corresponding literal 'I' to result , result =XXVI, N = 1-1=0

**Result = XXVI**

Submitted by Khushboo Jagwani (Khush123)

Download packets of source code on Coders Packet

## Comments