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.
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...
Design and Write a program that asks for a 4-digit year and determines whether that year...
Design and Write a program that asks for a 4-digit year and determines whether that year is a leap year or not. This should work for any year from 1700 to 2022. Anything not completely obvious to a newbie must use # notes on lines to explain. Must be modularized and repeatable. Should have an invocation of the main routine at the end of the program. Must have Flowgorithm and Python code.
In c++ language, write a void function GetYear that prompts for and inputs the year the...
In c++ language, write a void function GetYear that prompts for and inputs the year the operator was born (type int) from standard input. The function returns the user’s birth year through the parameter list (use pass by reference) unless the user enters an invalid year, in which case a BadYear exception is thrown. To test for a bad year, think about the range of acceptable years. It must be 4 digits (i.e. 1982) and it cannot be greater than...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT