fork download
  1. //Devin Scheu CS1A Chapter 4, P. 220, #2
  2. //
  3. /**************************************************************
  4. *
  5. * CONVERT NUMBER TO ROMAN NUMERAL
  6. * ____________________________________________________________
  7. * This program asks for a number between 1 and 10
  8. * and displays its Roman numeral equivalent.
  9. *
  10. * Computation is based on a switch statement mapping numbers to
  11. * Roman numerals: 1=I, 2=II, 3=III, 4=IV, 5=V, 6=VI, 7=VII,
  12. * 8=VIII, 9=IX, 10=X
  13. * ____________________________________________________________
  14. * INPUT
  15. * number : The number to convert (as an integer)
  16. *
  17. * OUTPUT
  18. * romanNumeral : The Roman numeral equivalent of the input number
  19. *
  20. **************************************************************/
  21.  
  22. #include <iostream>
  23. #include <iomanip>
  24.  
  25. using namespace std;
  26.  
  27. int main () {
  28.  
  29. //Variable Declarations
  30. int number; //INPUT - The number to convert (as an integer)
  31. string romanNumeral; //OUTPUT - The Roman numeral equivalent of the input number
  32.  
  33. //Prompt for Input
  34. cout << "Enter a number between 1 and 10: ";
  35. cin >> number;
  36.  
  37. //Input Validation and Conversion
  38. if (number < 1 || number > 10) {
  39. romanNumeral = "Invalid input";
  40. } else {
  41. switch (number) {
  42. case 1:
  43. romanNumeral = "I";
  44. break;
  45. case 2:
  46. romanNumeral = "II";
  47. break;
  48. case 3:
  49. romanNumeral = "III";
  50. break;
  51. case 4:
  52. romanNumeral = "IV";
  53. break;
  54. case 5:
  55. romanNumeral = "V";
  56. break;
  57. case 6:
  58. romanNumeral = "VI";
  59. break;
  60. case 7:
  61. romanNumeral = "VII";
  62. break;
  63. case 8:
  64. romanNumeral = "VIII";
  65. break;
  66. case 9:
  67. romanNumeral = "IX";
  68. break;
  69. case 10:
  70. romanNumeral = "X";
  71. break;
  72. }
  73. }
  74.  
  75. //Output Result
  76. cout << "\n";
  77. cout << left << setw(25) << "Entered Number:" << right << setw(15) << number << endl;
  78. cout << left << setw(25) << "Roman Numeral:" << right << setw(15) << romanNumeral << endl;
  79.  
  80. } //end of main()
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
Enter a number between 1 and 10: 
Entered Number:                        0
Roman Numeral:             Invalid input