Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class RomeNumbers {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter the number you want to convert: ");
- int num = scan.nextInt();
- if(num >= 1 && num <= 3999) {
- String romeNum = "";
- while (num != 0) {
- if (num - 1000 >= 0) {
- num -= 1000;
- romeNum += "M";
- } else if (num - 900 >= 0) {
- num -= 900;
- romeNum += "CM";
- } else if (num - 500 >= 0) {
- num -= 500;
- romeNum += "D";
- } else if (num - 400 >= 0) {
- num -= 400;
- romeNum += "CD";
- } else if (num - 100 >= 0) {
- num -= 100;
- romeNum += "C";
- } else if (num - 90 >= 0) {
- num -= 90;
- romeNum += "XC";
- } else if (num - 50 >= 0) {
- num -= 50;
- romeNum += "L";
- } else if (num - 10 >= 0) {
- num -= 10;
- romeNum += "X";
- } else if (num - 9 >= 0) {
- num -= 9;
- romeNum += "IX";
- }else if (num - 5 >= 0) {
- num -= 5;
- romeNum += "V";
- } else if (num - 4 >= 0) {
- num -= 5;
- romeNum += "IV";
- }else if (num - 1 >= 0) {
- num -= 1;
- romeNum += "I";
- }
- }
- System.out.println(romeNum);
- }
- else{
- System.out.println("Invalid number!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment