write a program in c++ to convert numbers 1-99 into roman numerals?

program in c++ that the user enters a number from 1-99 and it out puts the roman numeral

2 Answers

Relevance
  • Siju
    Lv 4
    7 years ago
    Favorite Answer

    // Converts an integer value into Roman numerals

    public string NumberToRoman(int number)

    {

    }

    // Validate

    if (number < 0 || number > 3999)

    throw new ArgumentException("Value must be in the range 0 - 3,999.");

    if (number == 0) return "N";

    // Set up key numerals and numeral pairs

    int[ ] values = new int[] { 1000, 900, 500, 400, 100,90, 50, 40, 10, 9, 5, 4, 1 };

    string[ ] numerals = new string[ ]

    { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };

    // Initialise the string builder

    StringBuilder result = new StringBuilder();

    // Loop through each of the values to diminish the number

    for (int i = 0; i < 13; i++)

    {

    // If the number being converted is less than the test value, append

    // the corresponding numeral or numeral pair to the resultant string

    while (number >= values[i])

    {

    number -= values[i];

    result.Append(numerals[i]);

    }

    }

    // Done

    return result.ToString();

    • Login to reply the answers
  • Bob
    Lv 4
    7 years ago

    include <iostream>

    using namespace std;

    int main()

    {

    int number;

    string numberInRomanNumeral;

    do

    {

    cout << "Enter a number between 1 and 99 (inclusive): ";

    cin >> number;

    } while(number < 1 || number > 99);

    if(number >= 10 && number < 20)

    numberInRomanNumeral = "X";

    else if(number >= 20 && number < 30)

    numberInRomanNumeral = "XX";

    else if(number >= 30 && number < 40)

    numberInRomanNumeral = "XXX";

    else if(number >= 40 && number < 50)

    numberInRomanNumeral = "XL";

    else if(number >= 50 && number < 60)

    numberInRomanNumeral = "L";

    else if(number >= 60 && number < 70)

    numberInRomanNumeral = "LX";

    else if(number >= 70 && number < 80)

    numberInRomanNumeral = "LXX";

    else if(number >= 80 && number < 90)

    numberInRomanNumeral = "LXXX";

    else if(number >= 90 && number < 100)

    numberInRomanNumeral = "XC";

    if(number > 10)

    number = number % 10;

    if(number == 1)

    numberInRomanNumeral += "I";

    else if(number == 2)

    numberInRomanNumeral += "II";

    else if(number == 3)

    numberInRomanNumeral += "III";

    else if(number == 4)

    numberInRomanNumeral += "IV";

    else if(number == 5)

    numberInRomanNumeral += "V";

    else if(number == 6)

    numberInRomanNumeral += "VI";

    else if(number == 7)

    numberInRomanNumeral += "VII";

    else if(number == 8)

    numberInRomanNumeral += "VIII";

    else if(number == 9)

    numberInRomanNumeral += "IX";

    cout << numberInRomanNumeral << endl;

    system("pause");

    return 0;

    }

    • Login to reply the answers
Still have questions? Get your answers by asking now.