Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.util.Scanner;
- public class homework {
- public static void main(String[] args) throws IOException {
- Scanner scan= new Scanner(System.in);
- int input = Integer.parseInt(scan.nextLine());
- System.out.println(numberParsing(input));
- }
- public static String numberParsing(int value) {
- if (value < 1 || value > 3999) {
- return "Invalid number";
- }
- StringBuilder s = new StringBuilder();
- while (value >= 1000) {
- s.append("M");
- value -= 1000;
- }
- while (value >= 900) {
- s.append("CM");
- value -= 900;
- }
- while (value >= 500) {
- s.append("D");
- value -= 500;
- }
- while (value >= 400) {
- s.append("CD");
- value -= 400;
- }
- while (value >= 100) {
- s.append("C");
- value -= 100;
- }
- while (value >= 90) {
- s.append("XC");
- value -= 90;
- }
- while (value >= 50) {
- s.append("L");
- value -= 50;
- }
- while (value >= 40) {
- s.append("XL");
- value -= 40;
- }
- while (value >= 10) {
- s.append("X");
- value -= 10;
- }
- while (value >= 9) {
- s.append("IX");
- value -= 9;
- }
- while (value >= 5) {
- s.append("V");
- value -= 5;
- }
- while (value >= 4) {
- s.append("IV");
- value -= 4;
- }
- while (value >= 1) {
- s.append("I");
- value -= 1;
- }
- return s.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement