/**********************************************************/ // R. A. Hillyard // roman.cpp // last modified: 09/17/2001 // This program reads in integers from 1 to 5000 from the user and // prints out their roman numeral equivalents. For example 1997 // becomes MDCCCCLXXXXVII. A sentinel of 0 is used to exit the program // // The algorithm for the conversion is as follows: // (1) While the decimal number is greater than or equal to 1000, // we print an "M" and reduce the number by 1000. // (2) If the number is still 500 or larger, we print a "D" // and reduce the number by 500 // (3), (4), ...We continue in a similar fashion to print all // appropriate C's, L's, X's, V's and I's. /*********************************************************/ #include<iostream> using namespace std; int main() { cout << "This program converts decimal numbers from 1 to 5000 " << "to roman numerals.\nType in 0 to stop\n"; do { int decimal; //holds value entered by user cout << "\nNumber to convert? "; cin >> decimal; if(decimal == 0) break; //user choice to exit loop if((decimal > 5000) || (decimal < 0)) cout << "the number must be between 1 and 5000.\n"; else { cout << decimal << " converts to ----> "; while(decimal >= 1000) { cout << "M"; decimal -= 1000; } if(decimal >= 500) { cout << "D"; decimal -= 500; } while(decimal >= 100) { cout << "C"; decimal -= 100; } if(decimal >= 50) { cout << "L"; decimal -= 50; } while(decimal >= 10) { cout << "X"; decimal -= 10; } if(decimal >= 5) { cout << "V"; decimal -= 5; } while(decimal >= 1) { cout << "I"; decimal--; } cout << endl; }//end else }while(true); return 0; }//end main /********************Program Output****************** This program converts decimal numbers from 1 to 5000 to roman numerals. Type in 0 to stop Number to convert? 1997 1997 converts to ----> MDCCCCLXXXXVII Number to convert? 50054 the number must be between 1 and 5000. Number to convert? 5000 5000 converts to ----> MMMMM Number to convert? 4995 4995 converts to ----> MMMMDCCCCLXXXXV Number to convert? 25 25 converts to ----> XXV Number to convert? 49 49 converts to ----> XXXXVIIII Number to convert? 0 */