Question

In: Computer Science

I need an idea of Java code that will convert an integer (1 to 3,999) into...

I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.

Solutions

Expert Solution

import java.util.Scanner;
public class IntToRoman {
   public static void main(String[] args) {
       //taking input from the user
       Scanner sc = new Scanner (System.in);
   System.out.print("Enter the integer: ");
   int num = sc.nextInt();
   //checking the condition to make sure the number between 1 to 3999
   if ((num < 1 || (num > 3999))) System.out.println("INVALID");
   else System.out.println(convertToRoman(num));
   }
   //logic to convert integer to roman without using arrays or loops
   public static String convertToRoman(int num){
   if (num >= 1000) return "M" + convertToRoman(num - 1000);
   if (num >= 900) return "CM" + convertToRoman(num - 900);
   if (num >= 500) return "D" + convertToRoman(num - 500);
   if (num >= 400) return "CD" + convertToRoman(num - 400);
   if (num >= 100) return "C" + convertToRoman(num - 100);
   if (num >= 90) return "XC" + convertToRoman(num - 90);
   if (num >= 50) return "L" + convertToRoman(num - 50);
   if (num >= 40) return "XL" + convertToRoman(num - 40);
   if (num >= 10) return "X" + convertToRoman(num - 10);
   if (num >= 9) return "IX" + convertToRoman(num - 9);
   if (num >= 5) return "V" + convertToRoman(num - 5);
   if (num >= 4) return "IV" + convertToRoman(num - 4);
   if (num >= 1) return "I" + convertToRoman(num - 1);
       return "";
   }
}


Related Solutions

JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
I need to convert the following into C++. The general idea is to create an application...
I need to convert the following into C++. The general idea is to create an application that can support 10 ID's and 10 grades. It needs to calculate the grade average of said ID, and determine if it is an below or above a certain average, ergo A or C. string[10] studentIDArray int[10] gradeArray int averageGrade for(int i = 0; i < gradeArray; i++) { averageGrade += gradeArray[i] } averageGrade = averageGrade / sizeof(gradeArray) for(int i = 0; i <...
i need a pseudocode for a program in java that will convert dollars into euros and...
i need a pseudocode for a program in java that will convert dollars into euros and japanese yen using the print and prinln methods an if, if -else, statement a switch statement a while statement utilizes the file class uses the random class and random number generator and uses at least three methods other than main
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
C++ Hello .I need to convert this code into template and then test the template with...
C++ Hello .I need to convert this code into template and then test the template with dynamic array of strings also if you can help me move the function out of the class that would be great.also There is a bug where the memory was being freed without using new operator. I cant seem to find it thanks in advance #include using namespace std; class DynamicStringArray {    private:        string *dynamicArray;        int size;    public:   ...
i need code in javascript or htmlt convert 0 to 999 numbers into word
i need code in javascript or htmlt convert 0 to 999 numbers into word
Java language (a) Write code segments to perform the following: (i) declare and create an integer...
Java language (a) Write code segments to perform the following: (i) declare and create an integer array freqArray of size 8 (ii) declare and initialize an array weight (with suitable type) which contains 48.5, 80 and 68 (iii) declare a Mouse array of size 2 with name mouse and initialize it with Mouse objects using one statement (b) A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3};...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT