how to convert a numeral to roman

user should read any numeral like 6931 from keyboard and output should be displayed in romans.
Start from either direction (although from the right it's slightly easier) and convert each digit to its roman representation.
Using the % and / operators you can get the last digit and shrink the number. Keep track your location and output the corresponding Roman Numeral.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
std::string romanstr;
int remainder;
int level = 0;
while ( val != 0 )
{
  remainder = val % 10;
  val /= 10;
  
  if (level == 0)
    switch (remainder) // Look at remainder
    {
      case 1: // Add data corresponding to remainder to string as so:
        romanstr = "I" + romanstr;
        break;
      case 2:
        romanstr = "II" + romanstr;
        break;
      case 3:
        romanstr = "III" + romanstr;
        break;
      case 3:
        romanstr = "IV" + romanstr;
        break;
      default:
    }
    if (level == 1) // Tens
      switch ()
    // Repeat pattern
    
    
    // At the end of all this After the while loop, cout << romanstr
}
While we are at it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
static string convertToRomanSmall(long n) //helper function for numbers below 4000
{
  string romanNumber;
  static const char romanDigits[]={'I','V','X','L','C','D','M'};
  for (int i=0;n>0;i++)
  {
    const long d=n%10; n/=10;
    string rd;
    if (d>=1 && d<=3)rd=string(d,romanDigits[i*2]);
    else if (d==4)rd=string(1,romanDigits[i*2])+romanDigits[i*2+1];
    else if (d>=5 && d<=8)rd=romanDigits[i*2+1]+string(d-5,romanDigits[i*2]);
    else if (d==9)rd=string(1,romanDigits[i*2])+romanDigits[i*2+2];
    romanNumber.insert(0,rd);
  }
  return romanNumber;
}

string convertToRoman(long n)
{
  if (n>=4000)return "("+convertToRoman((n-n%4000)/1000)+")"+convertToRomanSmall(n%4000);
  else return convertToRomanSmall(n);
}

Last edited on
closed account (z05DSL3A)
Hint:
use somthing like:
1
2
const string roman[13] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
const int decimal[13] =  {1000,  900, 500,  400, 100,   90,  50,   40,  10,   9,    5,    4,   1};

and a while loop inside a for loop with subtraction.

Edit
As Athar has already given you a solution, here is another:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;
 
string decimal2roman(int input) 
{
    string romanvalue = "";
    if(input >= 4000)
    {
        int x = (input - input % 4000) / 1000;
        romanvalue = "(" + decimal2roman(x) + ")" ;
        input %= 4000;
    }

    const string roman[13] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    const int decimal[13] =  {1000,  900, 500,  400, 100,   90,  50,   40,  10,   9,    5,    4,   1};
  
    for (int i = 0; i < 13; i++) 
    {
        while (input >= decimal[i]) 
        {
            input -= decimal[i];
            romanvalue += roman[i];
        }
    }
    return romanvalue;
}

int main()
{
    cout << decimal2roman(1899) << endl;
    cout << decimal2roman(4000) << endl;
    cout << decimal2roman(4564789) << endl;
    return 0;
}
MDCCCXCIX
(IV)
((IV)DLXIV)DCCLXXXIX
Last edited on
Topic archived. No new replies allowed.