In: Computer Science
in Java
Write a function that inputs a 4 digit year and outputs the
ROMAN numeral year
M is 1,000
C is 100
L is 50
X is 10
I is 1
Test with 2016 and 1989
public class NumberToRoman { public static String convertToRoman(int num) { String[] romanLetters = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"}; int[] romanValues = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000}; String romanOutput = ""; int quotient = 0; for (int i = romanValues.length - 1; i >= 0; i--) { if (num >= romanValues[i]) { quotient = num / romanValues[i]; while (quotient != 0) { romanOutput = romanOutput + romanLetters[i]; quotient--; } num = num % romanValues[i]; } } return romanOutput; } public static void main(String[] args) { System.out.println(convertToRoman(2016)); System.out.println(convertToRoman(1989)); } }