answersLogoWhite

0


Best Answer

There is a very good article on performing conversions from integers to roman numerals here: http://www.faqs.org/docs/javap/c9/ex-9-3-answer.html The article includes the full source code necessary to implement this solution on your own.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Java program that converts a decimal number to Roman number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is c program of roman numbers?

There being only 7 symbols to consider (IVXLCDM), conversion is easily achieved in any number of ways. In general, numerals are formed from left to right, largest value to smallest. However, if a smaller value precedes a larger value, the smaller value is subtracted from the larger value (or is negated). This can lead to problems such as IVX. Reading left to right this would become 10 - ( 5 - 1 ) = 10 - 4 = 6. There's nothing wrong with this, but most people would accept 6 = VI, not IVX. The problem is there has never been an official standard relating to how Roman numerals are formed. Decimal 1999 could be represented as MCMXCIX or MIM or MDCCCCLXXXXVIIII or even a mixed format like MCMXCVIIII. All are intrinsically correct. However, only the first example conforms to what many would consider to be the "unofficial" standard, whereby certain combinations are no longer permitted (such as IIII, IM and VX). This standard has been incorporated into the following code. #include <iostream> using namespace std; int main() { char roman[11]; int decimal[10]; memset( roman, 0, 11 ); memset( decimal, 0, 10 * sizeof( int )); cout << endl; cout << "Enter a Roman number (max. 10 chars from I, V, X, L, C, D or M): "; cin.getline( roman, 11, '\n' ); strupr( roman ); // convert to uppercase for consistency // check validity, including all invalid combinations if( !strlen( roman ) ( strspn( roman, "IVXLCDM") != strlen( roman )) ( strstr( roman, "IIII" )) ( strstr( roman, "XXXX" )) ( strstr( roman, "CCCC" )) ( strstr( roman, "MMMM")) ( strstr( roman, "IL" )) ( strstr( roman, "IC" )) ( strstr( roman, "ID" )) ( strstr( roman, "IM" )) ( strstr( roman, "XD" )) ( strstr( roman, "XM" )) ( strstr( roman, "VX" )) ( strstr( roman, "VL" )) ( strstr( roman, "VC" )) ( strstr( roman, "VD" )) ( strstr( roman, "VM" )) ( strstr( roman, "LC" )) ( strstr( roman, "LD" )) ( strstr( roman, "LM" )) ( strstr( roman, "DM" )) ( strstr( roman, "IIV" )) ( strstr( roman, "IIX" )) ( strstr( roman, "XXL" )) ( strstr( roman, "XXC" )) ( strstr( roman, "CCD" )) ( strstr( roman, "CCM" ))) { cout << roman << " is not a valid roman number." << endl; return( -1 ); } // convert to decimal, in reverse order. int c = 9, total = 0; while( c >= 0 ) { switch( roman[c] ) { case('I'): decimal[c] = 1; break; case('V'): decimal[c] = 5; break; case('X'): decimal[c] = 10; break; case('L'): decimal[c] = 50; break; case('C'): decimal[c] = 100; break; case('D'): decimal[c] = 500; break; case('M'): decimal[c] = 1000; break; } if( c < 9 ) // subtraction required? if( decimal[c] < decimal[c+1] ) decimal[c] *= (-1); // negate // update total. total += decimal[c--]; } cout << "Roman " << roman << " is decimal " << total << endl; return( 0 ); }


Why do computers work with bits rather than decimal digits?

Ever wonder what the real numbers are? Numbers are artificial things invented by human, and the same applied to computers. So, the inventors of computers storing human readable numbers (decimal, Roman numerals, etc...) as computer readable numbers (binary). Binary fit very well with the electrical pulses (on and off, as 1 and 0)


Four examples of Roman technology?

The roman technologies are: nine aqueducts to provide water for the roman people, amphitheaters, the Colosseum, and the romans greatest achievement was the sewer system.


What are the roman achievements in engineering?

hi


Whats the HTML code for roman numerals for a webpage?

To write a Roman Numeral you simply type in the letters in the text (4) IV. if you want an ordered list using Roman Numerals use the following list tags: A basic list using bullets (small black circles) starts with the tag and each item listed with the tags item one item two (end list) will result in Item one item two For a Ordered numbered listinstead of a bullet list use (ordered list) instead of (unordered list) to start. for example item oneitem two This will result in: # item one # item two For a Roman Numeral list you have to use the Type= attribute with the value "I" (capital i) inside the list start tag as follows, This will give you Roman Numerals For example: First Roman Numeral Item ISecond Roman Numeral Item II Third Roman Numeral  Item III The capital "I" will give you the Roman Numerals in capitals. If you use a lower case "i" the Roman Numerals will all be in lower case.