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

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:   ...
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...
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 CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
I need this code translated from C++ to Java. Im personally still trying to learn Java,...
I need this code translated from C++ to Java. Im personally still trying to learn Java, so if you can include screenshots of your IDE/output that would be helpful. Much appreciated! #include <iostream> #include <string> using namespace std; class pizza { public:    string ingrediants, address;    pizza *next;    pizza(string ingrediants, string address)    {        this->address = address;        this->ingrediants = ingrediants;        next = NULL;    } }; void enqueue(pizza **head, pizza **tail, pizza...
I need the JAVA code for a 4 function calculator app on andriod studio - The...
I need the JAVA code for a 4 function calculator app on andriod studio - The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button - Implement the onclicklistener on the main activity - The calcuator should use order of operations (PEMDAS) - It should be able to continue from a previous answer (Ex: If you type 2+6 the calculator will display 8. If you then multiple by...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
I need the output of the code like this in java First we create a new...
I need the output of the code like this in java First we create a new building and display the result: This building has no apartments. Press enter to continue......................... Now we add some apartments to the building and display the result: This building has the following apartments: Unit 1 3 Bedroom Rent $450 per month Currently unavailable Unit 2 2 Bedroom Rent $400 per month Currently available Unit 3 4 Bedroom Rent $1000 per month Currently unavailable Unit 4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT