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...
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.
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,...
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.
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...
Java question- Write a java program to process the number.txt file. Then count the numbers and...
Java question- Write a java program to process the number.txt file. Then count the numbers and calculate the total average, even numbers average, odd number average, then print the corresponding information. The result should be displayed in the following format there are XX numebers in the file there are xx even numbers there are xx odd numbers the total number average is xx the odd number average is xx the even number average is xx I am having trouble using...
in a gui ' in java write a program that draws equal a simple fence with...
in a gui ' in java write a program that draws equal a simple fence with vertical, spaced slats backed by two boards. Behind the fence show a simple house support Make sure the in the und. house is visible between the slats in the fence.
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT