Question

In: Computer Science

in Java Write a function that inputs a 4 digit year and outputs the ROMAN numeral...

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

Solutions

Expert Solution

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));
    }
}


Related Solutions

Write a simple java program to list roman numeral for a given range of numbers. Roman...
Write a simple java program to list roman numeral for a given range of numbers. Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M. I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 Roman numerals are usually written largest to smallest from left to right. But a number like 4 is not written as IIII. It is written as IV. Because...
Write a recursive function that converts Old Roman Numeral to base 10 values.
C++ program Write a recursive function that converts Old Roman Numeral to base 10 values. Print out the Old Roman Number and it base 10 equivalents. You will read all the digits of a Roman number into an array of characters. You will process each Roman digit in the array using ONLY POINTERS. Putting a ‘\0’ at the end of the input characters allows you to print the string easier. Use the following input to test your function:    DVIII, MMMDCCCCLXXXXIIII,...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a message and an integer n = pq where p and q are odd primes and an integer e > 1 relatively prime to (p − 1)(q − 1), encrypt the message using the RSA cryptosystem with key (n, e).
Using PHP, design a function that given a Roman numeral (Links to an external site.)Links to...
Using PHP, design a function that given a Roman numeral (Links to an external site.)Links to an external site. in input, is able to compute the modern Hindu–Arabic numeral (Links to an external site.)Links to an external site. system representation (aka, 0123456789). For example: Input Output VI 6 IV 4 MCMXC 1990 IX 9 Be sure to add all the necessary checks and at least one testing function. Try to split the logic in more small functions.
Using PHP, design a function that given a Roman numeral (Wikipedia link.), in input, is able...
Using PHP, design a function that given a Roman numeral (Wikipedia link.), in input, is able to compute the modern Hindu–Arabic numeral (wikipedia link), system representation (aka, 0123456789). For example: Input Output VI 6 IV 4 MCMXC 1990 IX 9 Be sure to add all the necessary checks and at least one testing function. Try to split the logic in more small functions.
why the production function evaluates the relation between the inputs and outputs is important in the...
why the production function evaluates the relation between the inputs and outputs is important in the healthcare field.
The production function evaluates the relation between the inputs and outputs is important in the healthcare...
The production function evaluates the relation between the inputs and outputs is important in the healthcare field.
Using python Create a function that inputs a list of numbers and outputs the median of...
Using python Create a function that inputs a list of numbers and outputs the median of the numbers. sort them and them show the output.
Write a java program MyCalendar that outputs a monthly calendar for a given month and year....
Write a java program MyCalendar that outputs a monthly calendar for a given month and year. the output should be looks like this one at the bottom:( I don't know why it doesn't show right but the first day of the month is not Sunday it starts from Wednesday) F. Last’s My Calendar Enter month year? 10 2019 10/2019 Sun Mon Tue Wed Thu Fri Sat --------------------------------- 1 2 3 4 5 6 7 8 9 10  11 12 13 14...
Write a java program The last digit of a credit card number is the check digit,...
Write a java program The last digit of a credit card number is the check digit, which protects againts transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Follwing steps explains the algorithm in determining if a credit card number is a valid card.  Starting from the right most digit, form the sum of every other digit. For example, if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT