Sunday 26 August 2012

java program convert roman to numeric


JAVA PROGRAM TO CONVERT ROMAN TO NUMERIC



public class RomantoDec {

public static void main(String[] args){

int M=1000,D=500,C=100,L=50,X=10,V=5,I=1;
int decimal=0;
String roman = args[0];
String romanNumeral = roman.toUpperCase();
String comRom[] ={"IV","IX","XL","XC","CD","CM"};
String genRom[] = new String[romanNumeral.length()-1];
for(int i=0,j=1;i<romanNumeral.length()&&j<romanNumeral.length();i++,j++){
genRom[i]= Character.toString(romanNumeral.charAt(i)) + Character.toString(romanNumeral.charAt(j));
}
for(int k=0;k<genRom.length;k++){
//System.out.print(genRom[k]+"\t");
String romo=genRom[k];
if(romo.equals("IV")){
decimal += V-I;
decimal = decimal-I;
decimal = decimal-V;
}
if(romo.equals("IX")){
decimal += X-I;
decimal = decimal-I;
decimal = decimal-X;
}
if(romo.equals("XL")){
decimal += L-X;
decimal = decimal-X;
decimal = decimal-L;
}
if(romo.equals("XC")){
decimal += C-X;
decimal = decimal-X;
decimal = decimal-C;
}
if(romo.equals("CD")){
decimal += D-C;
decimal = decimal-C;
decimal = decimal-D;
}
if(romo.equals("CM")){
decimal += M-C;
decimal = decimal-C;
decimal = decimal-M;
}
//}
}
int x = 0;
do {
char convertToDecimal = romanNumeral.charAt(x);
switch (convertToDecimal) {
case 'M':
decimal += M;
break;

case 'D':
decimal += D;
break;

case 'C':
decimal += C;
break;

case 'L':
decimal += L;
break;

case 'X':
decimal += X;
break;

case 'V':
decimal += V;
break;

case 'I':
decimal += I;
break;
}
x++;
} while (x < romanNumeral.length());
System.out.println("Decimal Number is: " + decimal);
}

}

Java program Numeric to Roman

WRITE A JAVA PROGRAM CONVERT NUMERIC TO ROMAN

convert numeric value to roman value,numeric to roman this is good program and good logic to convert a numeric to roman


class Roman
{
int n;
void set(int x)
{
n=x;
}
void convert()
{
if (n<=0)
{
System.out.println("no roman equivalant");
}
else
{
while(n>=1000)
{
System.out.print("M");
n=n-1000;
}
if(n>=900)
{
System.out.print("CM");
n=n-900;
}
if(n>=500)
{
System.out.print("D");
n=n-500;
}
if(n>=400)
{
System.out.print("CD");
n=n-400;
}
while(n>=100)
{
System.out.print("C");
n=n-100;
}
if(n>=90)
{
System.out.print("XC");
n=n-90;
}
if(n>=50)
{
System.out.print("L");
n=n-50;
}
if(n>=40)
{
System.out.print("XL");
n=n-40;
}
while(n>=10)
{
System.out.print("X");
n=n-10;
}
if(n>=9)
{
System.out.print("IX");
n=n-9;
}
if(n>=5)
{
System.out.print("V");
n=n-5;
}
if(n>=4)
{
System.out.print("IV");
n=n-4;
}
while(n>=1)
{
System.out.print("I");
n=n-1;
}


}
}
}

class RomanDemo
{
public static void main(String[] args)
{
if (args.length!=1)
{
System.out.println("plz enter only one value");
}
else
{
int x=Integer.parseInt(args[0]);
Roman r=new Roman();
r.set(x);
r.convert();
}
}
}