Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

Roman to integer

string[] s = { "I", "V", "X", "L", "C", "D", "M" };
int[] r = { 1, 5, 10, 50, 100, 500, 1000 };
string roman;
roman = Console.ReadLine();
int n = 0;
for (int i = 0; i < roman.Length; i++)
{
  for (int j = 0; j < s.Length; j++)
  {
    if (roman[i].ToString() == s[j])
    {
      for (int k = 0; k < s.Length; k++)
      {
        if (i + 1 < roman.Length)
        {
          if (roman[i + 1].ToString() == s[k])
          {
            if (k > j)
            {
              n -= r[j];
            }
            else
            {
              n += r[j];
            }
            break;
          }
        }
        else
        {
          n += r[j];
          break;
        }
      }
      break;
    }
  }
}
Console.WriteLine(n);
 
PREVIOUS NEXT
Tagged: #Roman #integer
ADD COMMENT
Topic
Name
4+9 =