Pages

Labels

Write a program that converts a decimal number to Roman number.

import java.util.*;

public class Roman
{
    public static void main(String [] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a Decimal Number");
        int num = input.nextInt();//1954
        int i=0;
        String str="";
        String [] roman={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        int [] decimal={1000,900,500,400,100,90,50,40,10,9,5,4,1};
        if(num>0&&num<4000)
        {
            for(i=0;i<13;i++)
            {
                while(num>=decimal[i])
                {
                num= num-decimal[i];
                str=str+roman[i];
                }
            }
            System.out.println("Desired Number = "+str);
        }
        else
        {
            System.out.println("Number Out of range");
        }

    }
}