Question

In: Computer Science

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 the one is before the five, we subtract it making four.

The same principle applies to the number nine, which is written as IX.

There are six instances where subtraction is used:

I can be placed before V (5) and X (10) to make 4 and 9.

X can be placed before L (50) and C (100) to make 40 and 90.

C can be placed before D (500) and M (1000) to make 400 and 900.

To DO:

Given a range of values, convert all integers within the range (inclusive) to roman

numerals, printing them out with one roman numeral per line.

Convert all integers in the range from 1 to 3999 to roman numerals and them out with

one roman numeral per line.

Example of input/output:

enter two positive integers, smaller followed by larger:

2 6

Output should be roman numeral for all the integers in the range as:

II

III

IV

V

VI

Solutions

Expert Solution

Approach:

1) Store the roman values of thousands , hundreds, tens, ones into arrays , so that we can use these arrays when digit places are placed in different places

2) Divide the decimal number into digits (thousands , hundreds , tens , ones)

3)Print the corresponding roman value for all the  digits in the decimal number

Code:

import java.util.Scanner; // scanner class for standard i/o
class Main
{
  
static String decimalToRomanNumeral(int num) // method to convert the decimal numbe roman numeral
{
// we strore the roman values of the digits from 0 to 9   
// these arrays helpul when they are placed in different places
String thousand[] = {"", "M", "MM", "MMM"};
String hundred[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
String ten[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
String one[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
  
// we will divide the decimal number into digits like thousands, hundreds , tens , ones
// from thousands place print the corresponding roman value stoed in the string array m
// like wise repeat for all digits
// finally combine all the roman numeral strings to get the final roman numeral
String thousands = thousand[num/1000];
String hundereds = hundred[(num%1000)/100];
String tens = ten[(num%100)/10];
String ones = one[num%10];
String result = thousands + hundereds + tens + ones; // final roman numeral
return result; // returns result
}
public static void main(String []args) { // main method
Scanner sc = new Scanner(System.in); // scanner class
System.out.println("Enter two positive integers, smaller followed by larger:");
int n1 = sc.nextInt(); // user input n1
int n2 = sc.nextInt(); // user input n2
for(int i=n1;i<=n2;i++)
System.out.println(decimalToRomanNumeral(i));

//for(int i=1;i<=3999;i++)
//System.out.println(decimalToRomanNumeral(i));
}
}

Output:

Output for roman numerals between 1 and 3999 Some outputs i have attached here...


Related Solutions

java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
Write a Java program to generate random numbers in the following range a. 1 <=n <=...
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 JAVA PROGRAMMING
Conversion of numeral to roman number: Create a FLOWCHART and C++ PROGRAM that prompts the user...
Conversion of numeral to roman number: Create a FLOWCHART and C++ PROGRAM that prompts the user to enter a number (from 1-3000 only) then output the number and its corresponding roman numeral number. ***validate the input (it must be 1-3000 only) SAMPLE OUTPUT: Enter a Number: 0 Number should be from 1-3000 only (Terminated) Enter a Number: -5 Number should be from 1-3000 only (Terminated) Enter a Number: 3500 Number should be from 1-3000 only (Terminated) Enter a Number: 1994...
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,...
In a java program: a) Given the following list of numbers: 90 8 7 56 123...
In a java program: a) Given the following list of numbers: 90 8 7 56 123 235 9 1 653 trace the execution for: Selection Sort (only the first 5 steps) MergeSort. (b) Given the following list of numbers: 3 1 4 1 5 9 2 6 5 3 5 trace the execution for quicksort with median-of-three partitioning and a cutoff of 3.
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.
Given a list x = [2,3,5,6,7,8,9], write a python program that find those numbers which are...
Given a list x = [2,3,5,6,7,8,9], write a python program that find those numbers which are divisible by 3. Arrange these numbers in a list called “result”. Hint: similar to page 9 in the slides.
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT