Write a program that prompts for a number and displays it in Roman numerals (You
ID: 3557296 • Letter: W
Question
Write a program that prompts for a number and displays it in Roman numerals (You should be able to
handle numbers from 1 to 4999. You can check your program by comparing its answers to the answers
at http://www.onlineconversion.com/roman_numerals_advanced.htm.).
Explanation / Answer
public static String IntegerToRomanNumeral(int input) { if (input < 1 || input > 3999) return "Invalid Roman Number Value"; String s = ""; while (input >= 1000) { s += "M"; input -= 1000; } while (input >= 900) { s += "CM"; input -= 900; } while (input >= 500) { s += "D"; input -= 500; } while (input >= 400) { s += "CD"; input -= 400; } while (input >= 100) { s += "C"; input -= 100; } while (input >= 90) { s += "XC"; input -= 90; } while (input >= 50) { s += "L"; input -= 50; } while (input >= 40) { s += "XL"; input -= 40; } while (input >= 10) { s += "X"; input -= 10; } while (input >= 9) { s += "IX"; input -= 9; } while (input >= 5) { s += "V"; input -= 5; } while (input >= 4) { s += "IV"; input -= 4; } while (input >= 1) { s += "I"; input -= 1; } return s; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.