February 06, 2010

Decimal number to Roman Numeral Converter(Java program)

import java.util.*;
public class Roman_Numerals
{
static Scanner console=new Scanner(System.in);
public static void main()
{
int num;
int ctr;
int ones_place;
int tens_place;
int hundreds_place;
int thousands_place;
System.out.println("Enter your desired number (1-10,999)");
num=console.nextInt();
ones_place=num%10;
tens_place=num%100/10;
hundreds_place=num%1000/100;
thousands_place=num%10000/1000;
//thousand's place
if (num/10000==1)
System.out.print("XO");
else if (thousands_place==9)
System.out.print("MXO");
else if(thousands_place>5&&thousands_place<9)
{
System.out.print("XO");
for (ctr=0;ctr System.out.print("M");
}
else if (thousands_place==5)
System.out.print("VO");
else if (thousands_place==4)
System.out.print("IVO");
else if (thousands_place<4)
for (ctr=0;ctr System.out.print("M");

//hundred's place
if (hundreds_place==9)
System.out.print("CM");
else if(hundreds_place>5&&hundreds_place<9)
{
System.out.print("D");
for (ctr=0;ctr System.out.print("C");
}
else if (hundreds_place==5)
System.out.print("D");
else if (hundreds_place==4)
System.out.print("CD");
else if (hundreds_place<4)
for (ctr=0;ctr System.out.print("C");
//ten's place
if (tens_place==9)
System.out.print("XC");
else if(tens_place>5&&tens_place<9)
{
System.out.print("L");
for (ctr=0;ctr System.out.print("X");
}
else if (tens_place==5)
System.out.print("L");
else if (tens_place==4)
System.out.print("XL");
else if (tens_place<4)
for (ctr=0;ctr System.out.print("X");
//one's place
if (ones_place==9)
System.out.print("IX");
else if(ones_place>5&&ones_place<9)
{
System.out.print("V");
for (ctr=0;ctr System.out.print("I");
}
else if (ones_place==5)
System.out.print("V");
else if (ones_place==4)
System.out.print("IV");
else if (ones_place<4)
for (ctr=0;ctr System.out.print("I");


}

}

No comments:

Post a Comment