Converting Decimal Numbers into Roman Numbers

Wednesday, November 24, 2010 by: Andy Kurnia Prayoga Made

Description: This program was made with the Java programming language. By using TextPad compiler. This program serves to convert decimal numbers into roman numbers, for example 16 is converted into XVI. This program has limitations in the maximum value that can be changed ie below 5000. If the input above the 5000 error message will arise from this program. Here's the source code of this program. Good luck!



import java.util.*;

public class DesimalToRomawi {
public static void main (String args[])
{
int bil;
String tampil="";

System.out.println("===================================================");
System.out.println("PROGRAM KONVERSI ANGKA DESIMAL MENJADI ANGKA ROMAWI");
System.out.println("        created by Made AndyKurnia Prayoga         ");
System.out.println("===================================================");
System.out.println(" ");

  //inisialisasi berupa array
  String [] biasa = {"","I","II", "III", "IV", "V",  "VI", "VII", "VIII", "IX"};
  String [] sepuluh = {"", "X", "XX","XXX","XL"};
  String [] limapuluh = {"", "L", "LX", "LXX", "LXXX","XC"};
  String [] seratus =  {"", "C", "CC", "CCC", "CD"};
  String [] limaratus = {"", "D", "DC", "DCC", "DCCC", "CM"};
  String [] seribu =   {"", "M", "MM", "MMM", "Mv", "v"};

  // input dari keyboard
  System.out.print("Masukkan bilangan desimal : ");
  Scanner obj = new Scanner(System.in);
  bil = obj.nextInt();

  //kondisi dimana angka tidak lebih dari 5000
  if (bil>5000)
  {
  System.out.println("Maaf.., Angka Desimal Yang Di Inputkan Harus Di Bawah 5000");
  }
  else
  {
     int lmrts = bil % 1000;
     int srts = lmrts % 500;
     int lmpl = srts % 100;
     int spl = lmpl % 50;
     int ak = spl % 10;

     int a = bil/1000;
     tampil += ""+seribu[a];

     //aturan konversi desimal ke romawi
     if ( (lmrts >=900) && (lmrts <= 999))
     {
     tampil += "CM";

     //90
     if( (lmpl >= 90) && (lmpl <= 99) )
     {
     tampil += "XC";
     int f= ak/1;
     tampil += ""+biasa[f];
     }
     else
     {
     int d = lmpl/50;
     tampil += ""+limapuluh[d];
     int e = spl/10;
     tampil += ""+sepuluh[e];
     int f= ak/1;
     tampil += ""+biasa[f];
     }

     }

     else
     {
     int b = lmrts/500;
     tampil += ""+limaratus[b];

     //400
     if ( (srts >= 400)&& (srts <= 499) )
     {
     tampil += "CD";

     //90
     if( (lmpl >= 90) && (lmpl <= 99) )
     {
     tampil += "XC";
     int f= ak/1;
     tampil += ""+biasa[f];
     }
     else
     {
     int d = lmpl/50;
     tampil += ""+limapuluh[d];
     int e = spl/10;
     tampil += ""+sepuluh[e];
     int f= ak/1;
     tampil += ""+biasa[f];
     }

     }

     else
     {
     int c = srts/100;
     tampil += ""+seratus[c];

     if((lmpl >= 90)&&(lmpl <= 99))
     {
     tampil += "XC";
     int f= ak/1;
     tampil += ""+biasa[f];
     }
     else
     {
     int d = lmpl/50;
     tampil += ""+limapuluh[d];
     int e = spl/10;
     tampil += ""+sepuluh[e];
     int f= ak/1;
     tampil += ""+biasa[f];
     }
     }
   }

   System.out.println(" ");
   System.out.println("Maka angka Romawinya " +tampil);
   System.out.println(" ");
   System.out.println("========================================");
   System.out.println(" ");
   System.out.println("Konversi dari angka Desimal " +bil+ " adalah angka Romawina " +tampil);
   System.out.println(" ");

   System.out.println("===================================================");
   System.out.println("    TERIMA KASIH SUDAH MENGGUNAKAN PROGRAM INI     ");
   System.out.println("===================================================");

  }
  }
 }

Filed under: