Algorithms Used:
1. If multiples of 1000 replaced by the M
2. If the rest above 900 added CM
3. If the rest above 500 added D
4. If the rest above 400 added CD
5. If the rest multiples of 100 replaced with C //repeat multiplication algorithm of 1000
6. If the rest above 90 added XC
7. If the rest above 50 added L
8. If the rest above 40 added CL
9. If the rest multiples of 10 replaced with C //repeat multiplication algorithm of 1000 or 100
10. If the rest above 9 added IX
11. If the rest above 5 added V
12. If the rest above 4 added IV
13. The rest added I
For the above 1000 repeat multiplication algorithm of 1000 by find the number in Roman symbol
Implementation in Java Code:
In this coding it's used If Else If
Create a class with the name Conversion
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package inymgd; /** * * @author monday */ import java.io.*; public class Conversion { public static void main (String args[]) { try{ //variable declaration for number int number; do{ //show input stream System.out.print ("input decimal number (1-4000): "); //read input stream and add the value to number BufferedReader bfr = new BufferedReader (new InputStreamReader(System.in)); number = Integer.parseInt(bfr.readLine()); }while(number < 1 || number > 4000); //variable declaration for roman String roman = ""; //algorithm implementation do{ if (number >= 1000){ number = number - 1000; roman = roman + "M"; } else if (number >= 900){ number = number - 900; roman = roman + "CM"; } else if (number >= 500){ number = number - 500; roman = roman + "D"; } else if (number >= 400){ number = number - 400; roman = roman + "CD"; } else if (number >=100){ number = number - 100; roman = roman + "C"; } else if (number >=90){ number = number - 90; roman = roman + "XC"; } else if (number >=50){ number = number - 50; roman = roman + "L"; } else if (number >=40){ number = number - 40; roman = roman + "XL"; } else if (number >=10){ number = number - 10; roman = roman + "X"; } else if (number >=9){ number = number - 9; roman = roman + "IX"; } else if (number >=5){ number = number - 5; roman = roman + "V"; } else if (number >=4){ number = number - 4; roman = roman + "IV"; } else if (number >=1){ number = number - 1; roman = roman + "I"; } }while (number > 0); //show the result of conversion System.out.println("roman symbol: "+ roman); }catch (IOException io){ System.out.println ("Error: " + io.getMessage()); } } }
Tidak ada komentar:
Posting Komentar