Integer to Roman

By   Tewodros   Date Posted: Oct. 10, 2023  Hits: 411   Category:  .NET Core   Total Comment: 0             A+ A-


side

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Given a positive integer convert it in to its Roman number’s representation.

Example 1:

Input:

 num = 3

Output:

 "III"

Explanation:

 3 is represented as 3 ones.

Example 2:

Input:

 num = 58

Output:

 "LVIII"

Explanation:

 L = 50, V = 5, III = 3.

Example 3:

Input:

 num = 1994

Output:

 "MCMXCIV"

Explanation:

 M = 1000, CM = 900, XC = 90 and IV = 4.

Solution 

public string IntToRoman(int num) {

   string[] thousands = {"", "M", "MM", "MMM"};

   string[] hundreds = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};

   string[] tens = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};

   string[] ones = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};

 

   return thousands[num / 1000] +

          hundreds[(num % 1000) / 100] +

          tens[(num % 100) / 10] +

          ones[num % 10];

}

 

 

 


Tags



Back to Top



Related Blogs






Please fill all fields that are required and click Add Comment button.

Name:*
Email:*
Comment:*
(Only 2000 char allowed)


Security Code:* vgcrzv

Back to Top