Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting integers to roman numerals

I'm trying to write a function that converts numbers to roman numerals. This is my code so far; however, it only works with numbers that are less than 400. Is there a quick and easy way to do this conversion, or extend my existing code so that it handles all cases? Thanks in advance for any help.

static string convertroman(int number)     {         int l = number / 10;         StringBuilder sb = new StringBuilder();         for (int m = 0; m <= l; m++)         {             if (l == 0)             {                 break;             }             if (l == 5)             {                 sb = sb.Append(ro.L.ToString());                 break;             }             if (l == 4)             {                 sb = sb.Append(ro.X.ToString()).Append(ro.L.ToString());                 break;             }             if (l == 9)             {                 sb = sb.Append(ro.X.ToString()).Append(ro.C.ToString());                 break;             }             if (l == 10)             {                 sb = sb.Append(ro.C.ToString());                 break;             }              if (l > 5 && l < 9)             {                 sb = sb.Append(ro.L.ToString());                 l = l - 5;                 m = 0;                 // break;                 continue;             }             if (l > 10)             {                 sb = sb.Append(ro.C.ToString());                 l = l - 10;                 m = 0;                 // continue;              }             else             {                 sb = sb.Append(ro.X.ToString());             }          }         int z = number % 10;         for (int x = 0; x <= z; x++)         {             if (z == 0)             {                 break;             }             if (z == 5)             {                 sb = sb.Append(ro.V.ToString());                 break;             }             if (z == 4)             {                 sb = sb.Append(ro.I.ToString()).Append(ro.V.ToString());                 break;             }             if (z == 9)             {                 sb = sb.Append(ro.I.ToString()).Append(ro.X.ToString());                 break;             }             if (z == 10)             {                 sb = sb.Append(ro.X.ToString());                 break;             }             if (z > 5 && z < 9)             {                 sb = sb.Append(ro.V.ToString());                 z = z - 5;                 x = 0;             }             else             {                 sb.Append(ro.I.ToString());             }                        }         return sb.ToString();                } 
like image 633
Kavithova L Avatar asked Aug 12 '11 12:08

Kavithova L


People also ask

Can you convert words into roman numerals?

You need to simply type in the Unicode in a Word document without the 'U+' on its front. Tap and hold the "Alt" key after typing. Step 2. While holding the "Alt" key, press "X" to change it in the Roman numeral.

How do you convert int to roman numerals in C++?

C++ Code to convert integer to roman numeral At first, we create a map in which we put the numbers 0-9, 10, 20, 30, 40, .. 90, 100, 200, 300,… 900, 1000, 2000, 3000. We do this because as soon as we know how to write the above numbers in roman then essentially can write each and every number into its roman for.


1 Answers

Try this, simple and compact:

public static string ToRoman(int number) {     if ((number < 0) || (number > 3999)) throw new ArgumentOutOfRangeException("insert value betwheen 1 and 3999");     if (number < 1) return string.Empty;                 if (number >= 1000) return "M" + ToRoman(number - 1000);     if (number >= 900) return "CM" + ToRoman(number - 900);      if (number >= 500) return "D" + ToRoman(number - 500);     if (number >= 400) return "CD" + ToRoman(number - 400);     if (number >= 100) return "C" + ToRoman(number - 100);                 if (number >= 90) return "XC" + ToRoman(number - 90);     if (number >= 50) return "L" + ToRoman(number - 50);     if (number >= 40) return "XL" + ToRoman(number - 40);     if (number >= 10) return "X" + ToRoman(number - 10);     if (number >= 9) return "IX" + ToRoman(number - 9);     if (number >= 5) return "V" + ToRoman(number - 5);     if (number >= 4) return "IV" + ToRoman(number - 4);     if (number >= 1) return "I" + ToRoman(number - 1);     throw new ArgumentOutOfRangeException("something bad happened"); } 
like image 123
Mosè Bottacini Avatar answered Sep 30 '22 09:09

Mosè Bottacini