converting numbers to roman numerals - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


converting numbers to roman numerals

HI there, can anyone help me with a problem. I am in my first year in C++ programming class. How can i comvert a year between 1000 to 3000 in roman numerals. Please remember this is very new to me.
Here is two examples:
We have covered functions and are in the chapter of loops and flow control.
Thank you for your help



  • BriballBriball Posts: 265Member
    What we need to do is split the number into its parts. 1890 has 3 parts to it: 1000 + 800 + 90. 1963 gas 4: 1000 + 900 + 60 + 3. Since we are restricted to 1000-3000 we know we'll have 4 digits, with at least one "part" to convert.

    We could write a function responsible for converting any number "part" to its roman numeral equivelent.

    BOOL ConvertNumeral(int num, char *numeral)
    //cover the possibles for the first part
    if(num == 1000 && strlen(numeral) >= 1)
    strcpy(numeral, "M");
    else if(num == 2000 && strlen(numeral) >= 2)
    strcpy(numeral, "MM");
    else if(num == 3000 && strlen(numeral) >=3)
    strcpy(numeral, "MMM");

    //now for the possible second parts
    else if(num == 100 && strlen(numeral) >= 1)
    strcpy(numeral, "C");
    else if(num == 200 && strlen(numeral) >= 2)
    strcpy(numeral, "CC");
    else if(num == 300 && strlen(numeral >= 3)
    strcpy(numeral, "CCC");
    else if(num == 400 && strlen(numeral) >= 4)
    strcpy(numeral, "CCCC");
    else if(num == 500 && strlen(numeral >= 2)
    strcpy(numeral, "VC");
    ...//yada yada, you get the point here. Next you need to check the
    //tens digits, then the ones then you are done.

    //remember, the numeral string passed to this function should NOT
    //be the string storing the final answer, but a temp variable
    return TRUE if the roman numeral value was found, FALSE if not.

    now convert the original number to a convertable form. We need to find its "parts." Do this by storing the parts in an array, and dividing the original until we get what we want.

    int num = 1986, divisor = 1000;
    char roman[1024] = "";
    char numeral[16];

    for(int i = 0; i <= 3; i++){
    if(ConvertNumeral((num/divisor)*divisor, numeral))
    strcat(roman, numeral);

    num %= divisor;
    divisor /= 10;

Sign In or Register to comment.