Friday 27 March 2015

PROGRAM 9 : TO CONVERT A NUMBER ( NOT MORE THAN 3999) INTO A ROMAN NUMERAL

Example:


PROGRAM:

import java.util.Scanner;
public class decimaltoroman
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number to find out its ROMAN NUMERAL:(not more than 3999)");
        int num = sc.nextInt();
        String thou[]={"","M","MM","MMM"};
        String hund[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
        String ten[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
        String unit[]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
        if(num>3999)
          System.out.println("Sorry. number entered out of bound.");
        else
        {
          int th = num/1000; // DIVIDES NUMBER BY 1000 TO FIND THE THOUSANDTH DIGIT
          int h = (num/100)%10; // TO FIND THE HUNDRETH DIGIT
          int t = (num/10)%10; // TO FIND THE TENTH DIGIT
          int u = num%10; // TO FIND THE SINGLE LAST DIGIT
          System.out.println("ROMAN NUMERAL: " + thou[th] + hund[h] + ten[t] + unit[u]); // WILL REFER TO THE ARRAYS ABOVE TO GET THE ROMAN NUMBER
        }
    }
}
 

No comments:

Post a Comment