Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 2: (Roman numerals) Write a program that converts an integer between 1 a

ID: 3821426 • Letter: P

Question

Problem 2: (Roman numerals) Write a program that converts an integer between 1 and 3999 into Roman numerals. The input and output should be exactly: Input a number in Arabic numerals: CUSER ENTERS AN INTEGER BETWEEN 1 AND 3999] XXXXXX is your number in Roman numerals. se the "standard for described in https://en.wikipedia.org/wiki/Roman numerals Assume the input is indeed between 1 and 3999. You may not use any libraries aside from iostream and string. Name your file roman.cpp. Hint. Threat each decimal digit separately (i.e., do the thousands, hundreds, tens, and ones separ rately) and concatenate the results.

Explanation / Answer

#include <iostream>
#include <string>

using namespace std;

// To add corresponding base symbols in the array
// to handle cases which follow subtractive notation.
// Base symbols are added index 'i'.
int sub_digit(char num1, char num2, int i, char *c)
{
c[i++] = num1;
c[i++] = num2;
return i;
}

// To add symbol 'ch' n times after index i in c[]
int digit(char ch, int n, int i, char *c)
{
for (int j = 0; j < n; j++)
c[i++] = ch;
return i;
}

// Function to convert decimal to Roman Numerals
void printRoman(int number)
{
char c[10001];
int i = 0;

// If number entered is not valid
if (number <= 0)
{
cout << "Invalid number";
return;
}

// TO convert decimal number to roman numerals
while (number != 0)
{
// If base value of number is greater than 1000
if (number >= 1000)
{
// Add 'M' number/1000 times after index i
i = digit('M', number/1000, i, c);
number = number%1000;
}

// If base value of number is greater than or
// equal to 500
else if (number >= 500)
{
// To add base symbol to the character array
if (number < 900)
{
// Add 'D' number/1000 times after index i
i = digit('D', number/500, i, c);
number = number%500;
}

// To handle subtractive notation in case of number
// having digit as 9 and adding corresponding base
// symbol
else
{
// Add C and M after index i/.
i = sub_digit('C', 'M', i, c);
number = number%100 ;
}
}

// If base value of number is greater than or equal to 100
else if (number >= 100)
{
// To add base symbol to the character array
if (number < 400)
{
i = digit('C', number/100, i, c);
number = number%100;
}

// To handle subtractive notation in case of number
// having digit as 4 and adding corresponding base
// symbol
else
{
i = sub_digit('C','D',i,c);
number = number%100;
}
}

// If base value of number is greater than or equal to 50
else if (number >= 50 )
{
// To add base symbol to the character array
if (number < 90)
{
i = digit('L', number/50,i,c);
number = number%50;
}

// To handle subtractive notation in case of number
// having digit as 9 and adding corresponding base
// symbol
else
{
i = sub_digit('X','C',i,c);
number = number%10;
}
}
// If base value of number is greater than or equal to 10
else if (number >= 10)
{
// To add base symbol to the character array
if (number < 40)
{
i = digit('X', number/10,i,c);
number = number%10;
}

// To handle subtractive notation in case of
// number having digit as 4 and adding
// corresponding base symbol
else
{
i = sub_digit('X','L',i,c);
number = number%10;
}
}

// If base value of number is greater than or equal to 5
else if (number >= 5)
{
if (number < 9)
{
i = digit('V', number/5,i,c);
number = number%5;
}

// To handle subtractive notation in case of number
// having digit as 9 and adding corresponding base
// symbol
else
{
i = sub_digit('I','X',i,c);
number = 0;
}
}

// If base value of number is greater than or equal to 1
else if (number >= 1)
{
if (number < 4)
{
i = digit('I', number,i,c);
number = 0;
}

// To handle subtractive notation in case of
// number having digit as 4 and adding corresponding
// base symbol
else
{
i = sub_digit('I', 'V', i, c);
number = 0;
}
}
}

// Printing equivalent Roman Numeral
for (int j = 0; j < i; j++)
cout << c[j];
   cout << " is your number in Roman numerals" << endl;
}

//Driver program
int main()
{
int number;
   cout << "Input a number in Arabic numerals: ";
   cin >> number;

printRoman(number);

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote