Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Help me in C++ codes about arabic to roman numerals?
Hi can someone give me C++ code that converts arabic numerals to roman numerals in years;
for example
this sample output:
Enter Year (1000-3000): 1900
MCM
Repeat?(Y/N): Y
Enter Year (1000-3000): 1989
MCMCXXXIX
Repeat?(Y/N): N
need you help please ty
4 Answers
- Anonymous1 decade agoFavorite Answer
This code is really sloppy and probably not the best way of doing what you want, but it works:
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
string convertAtoR(int arabic)
{
int A_hundreds; // A stands for arabic
int A_tens;
int A_ones;
string thousands;
string hundreds;
string tens;
string ones;
string romanNumerals;
if (1000 <= arabic < 2000) {
thousands="M";
A_hundreds=floor((arabic-1000)/100)*100;
A_tens=floor((arabic-(1000+A_hundreds))/10)*10;
A_ones=floor(arabic-(1000+A_hundreds+A_tens));
}
if (arabic < 3000 && arabic >= 2000) {
thousands="MM";
A_hundreds=floor((arabic-2000)/100)*100;
A_tens=floor((arabic-(2000+A_hundreds))/10)*10;
A_ones=arabic-(2000+A_hundreds+A_tens);
}
if (arabic == 3000) {
romanNumerals="MMM";
return romanNumerals;
}
switch(A_hundreds) {
case 0:
hundreds="";
break;
case 100:
hundreds="C";
break;
case 200:
hundreds="CC";
break;
case 300:
hundreds="CCC";
break;
case 400:
hundreds="CD";
break;
case 500:
hundreds="D";
break;
case 600:
hundreds="DC";
break;
case 700:
hundreds="DCC";
break;
case 800:
hundreds="DCCC";
break;
case 900:
hundreds="CM";
break;
}
switch(A_tens) {
case 0:
tens="";
break;
case 10:
tens="X";
break;
case 20:
tens="XX";
break;
case 30:
tens="XXX";
break;
case 40:
tens="XL";
break;
case 50:
tens="L";
break;
case 60:
tens="LX";
break;
case 70:
tens="LXX";
break;
case 80:
tens="LXXX";
break;
case 90:
tens="XC";
break;
}
switch(A_ones) {
case 0:
ones="";
break;
case 1:
ones="I";
break;
case 2:
ones="II";
break;
case 3:
ones="III";
break;
case 4:
ones="IV";
break;
case 5:
ones="V";
break;
case 6:
ones="VI";
break;
case 7:
ones="VII";
break;
case 8:
ones="VIII";
break;
case 9:
ones="IX";
break;
}
romanNumerals=thousands+hundreds+tens+ones;
return romanNumerals;
}
int inOutNumerals()
{
char szNumber[20];
string yesNo;
string test="0";
while(test=="0") {
cout << "Enter Year (1000-3000): ";
cin >> szNumber;
int nNumber=atoi(szNumber);
if(!(isnan(nNumber) || nNumber<1000 || nNumber>3000)) {
cout << convertAtoR(nNumber) << "\n";
test="1";
}
else {
cout << "Invalid year" << endl;
test="0";
}
}
while(1) {
cout << "Repeat?(Y/N): ";
cin >> yesNo;
if(yesNo=="Y")
return 1;
else if(yesNo=="N")
return 0;
else
cout << "Please enter Y or N\n";
}
}
int main()
{
int returnValue;
while (1) {
returnValue=inOutNumerals();
if (returnValue==0) {
break;
}
}
cout << "Program End" << endl;
return 0;
}
Here is a sample output from this program:
Enter Year (1000-3000): 45
Invalid year
Enter Year (1000-3000): 1245
MCCXLV
Repeat?(Y/N): Y
Enter Year (1000-3000): qwerty
Invalid year
Enter Year (1000-3000): 5000
Invalid year
Enter Year (1000-3000): 2000
MM
Repeat?(Y/N): N
Program End
- ?Lv 71 decade ago
During the input loop, first verify that the user input is in the required range. Then, call a function named arabicToRoman, which will return a string representation in Roman numerals of the string argument in Arabic numerals.
string arabicToRoman(string year)
{
string sR[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int nA[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
int y = stoi(year);
string sRoman("");
for(int i = 0; i < 13; i++)
{
while(y >= nA[i])
{
sRoman += sR[i];
y -= nA[i];
}
}
return sRoman;
}
This uses the algorithm described in the source link (although the link implements the algorithm in Java instead of C++).
Thirteen "elementary Roman numerals" (which actually include combinations of Roman numerals, such as "XL" and "IX") are stored in a string array, while their corresponding Arabic values (e.g. 40 and 9) are stored in an int array.
The year is parsed and stored as an int y; a string sRoman is initialized as an empty string.
The loop cycles through the Arabic values; as long as y is greater than the current loop value, the loop value's corresponding Roman string is appended to sRoman, and the loop value is subtracted from y.
By the time y == 0, it has been fully converted to Roman numerals, and sRoman is returned.
This algorithm can be adjusted and improved. For instance, it seems to me that the year 1999 can be more adequately represented in Roman numerals as IMM, but this algorithm instead generates MCMXCIX.
This can be remedied by simply adding more "elementary Roman numerals" and their corresponding Arabic values to the arrays.
Good luck!
- Shadow WolfLv 61 decade ago
First you need to enumerate the various letters for roman numerals.
Next, you need to figure out what letter combinations you need to represent your number.
Following the roman numeral rules, put together the proper string.
Finally you need to display the number.
Since it repeats, you'll need to put all this inside a loop.
Give you code? Do I also get credit for doing your homework?
Shadow Wolf