0% found this document useful (0 votes)
106 views

Roman Numeral Code - Java

The document is a code sample in Java that converts numeric values to Roman numerals. It takes a number as input, uses if/else statements to check the numeric value and add the corresponding Roman numeral character to a string. It then prints the final Roman numeral string. There are also additional code snippets and comments provided for converting Roman numerals to numeric values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

Roman Numeral Code - Java

The document is a code sample in Java that converts numeric values to Roman numerals. It takes a number as input, uses if/else statements to check the numeric value and add the corresponding Roman numeral character to a string. It then prints the final Roman numeral string. There are also additional code snippets and comments provided for converting Roman numerals to numeric values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

// Hello.

I am from Philippines (LOL~) and I dont know if my codes are good enoug
h to show or readable for those who really needs to get answer this program that
is quite easy to understand . Thanks to (MouseEvent) because he gave me soo muc
h idea on how to do it but I will not using any other pre-defined methods or pac
kages or arrays because I hate it (LOL~ blame our professor who didn't want us t
o use pre-defined method/s) because I do not know how to use it. I am student so
I must study hard some advance in order to know your codes right up there.

import java.util.Scanner;
public class RomanNumeral
{
public static void main()
{
Scanner input = new Scanner (System.in);
int num;
String roman = "";
System.out.print("Enter a number to convert Roman Numeral: ");
num = input.nextInt();

if (num == 0)
{
System.out.println("Invalid roman number value!");
}
while (num != 0)
{
if (num < 4) // For 1 - 3 ('I')
{
roman = roman + "I";
num = num - 1;
}
else if (num == 4) // For 4 ('IV')
{
roman = roman + "IV";
num = num - 4;
}

else if (num < 9) // For 5 - 9 ('V')


{
roman = roman + "V";
num = num - 5;
}
else if (num == 9) // For 9 ('IX')
{
roman = roman + "IX";
num = num - 9;
}
else if (num < 40) // For 10 - 39 ('X')
{
roman = roman + "X";
num = num - 10;
}
else if (num == 40 || num < 50) // For 40 - 49 ('XL')
{
roman = roman + "XL";
num = num - 40;
}
else if (num < 90) // For 50 ('L')
{
roman = roman + "L";
num = num - 50;
}
else if (num == 90 || num < 100) // For 90 - 99 ('XC')
{
roman = roman + "XC";
num = num - 90;
}

else if (num < 400) // For 100 - 399 ('C')


{
roman = roman + "C";
num = num - 100;
}
else if (num == 400 || num < 500) // For 400- 499 ('CD')
{
roman = roman + "CD";
num = num - 400;
}
else if (num < 900) // For 500- 899 ('D')
{
roman = roman + "D";
num = num - 500;
}

else if (num == 900 || num < 1000) // For 900 - 999 ('CM')
{
roman = roman + "CM";
num = num - 900;
}
else if (num < 4000) // For 1000- 3999 ('M')
{
roman = roman + "M";
num = num - 1000;
}
else
{
System.out.println("Invalid roman number value!");
break;
}
}
System.out.println("Roman Numeral: " + roman);
}
}
// Is it my codes are okay or not?
Scanner input = new Scanner(System.in);
int magic = 0;
int[] numbers = new int[10];
for(int i = 0; i < 10; i++) {
System.out.println("Please enter a value: \n" + "[" + (i + 1) + "]: ");
numbers[i] = input.nextInt();
}
System.out.println("\nThe numbers you entered were: \n");
for(int i = 0; i < 10; i++) {
System.out.println(numbers[i] + ", ");
}
System.out.println("done.\n\n");
System.out.println("The unique numbers are: ");
magic = numbers[0];
System.out.println(magic + ", ");
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
if(numbers[i] == numbers[j] && j != i) {
numbers[j] = magic;
}
}
if(numbers[i] != magic) {
System.out.println(numbers[i] + ", ");
}
}
System.out.println("done.\n\n");
class Roman {
private static int decodeSingle(char letter) {
switch (letter) {
case 'M':
return 1000;
case 'D':
return 500;
case 'C':
return 100;
case 'L':
return 50;
case 'X':
return 10;
case 'V':
return 5;
case 'I':
return 1;
default:
return 0;
}
}
public static int decode(String roman) {
int result = 0;
String uRoman = roman.toUpperCase(); //case-insensitive
for (int i = 0; i < uRoman.length() - 1; i++) {//loop over all but the l
ast character
if (decodeSingle(uRoman.charAt(i)) < decodeSingle(uRoman.charAt(i +
1))) {
result -= decodeSingle(uRoman.charAt(i));
} else {
result += decodeSingle(uRoman.charAt(i));
}
}
result += decodeSingle(uRoman.charAt(uRoman.length() - 1));
return result;
}

down vote
accepted
It will be good if you traverse in reverse.
public class RomanToDecimal {
public static void romanToDecimal(java.lang.String romanNumber) {
int decimal = 0;
int lastNumber = 0;
String romanNumeral = romanNumber.toUpperCase();
/* operation to be performed on upper cases even if user
enters roman values in lower case chars */
for (int x = romanNumeral.length() - 1; x >= 0 ; x--) {
char convertToDecimal = romanNumeral.charAt(x);
switch (convertToDecimal) {
case 'M':
decimal = processDecimal(1000, lastNumber, decimal);
lastNumber = 1000;
break;
case 'D':
decimal = processDecimal(500, lastNumber, decimal);
lastNumber = 500;
break;
case 'C':
decimal = processDecimal(100, lastNumber, decimal);
lastNumber = 100;
break;
case 'L':
decimal = processDecimal(50, lastNumber, decimal);
lastNumber = 50;
break;
case 'X':
decimal = processDecimal(10, lastNumber, decimal);
lastNumber = 10;
break;
case 'V':
decimal = processDecimal(5, lastNumber, decimal);
lastNumber = 5;
break;
case 'I':
decimal = processDecimal(1, lastNumber, decimal);
lastNumber = 1;
break;
}
}
System.out.println(decimal);
}
public static int processDecimal(int decimal, int lastNumber, int lastDecima
l) {
if (lastNumber > decimal) {
return lastDecimal - decimal;
} else {
return lastDecimal + decimal;
}
}
public static void main(java.lang.String args[]) {
romanToDecimal("XIV");
}
}

private static int totalValue(String val)


{
String aux=val.toUpperCase();
int sum=0, max=aux.length(), i=0;
while(i<max)
{
if ((i+1)<max && valueOf(aux.charAt(i+1))>valueOf(aux.charAt(i)))
{
sum+=valueOf(aux.charAt(i+1)) - valueOf(aux.charAt(i));
i+=2;
}
else
{
sum+=valueOf(aux.charAt(i));
i+=1;
}
}
return sum;
}
private static int valueOf(Character c)
{
char aux = Character.toUpperCase(c);
switch(aux)
{
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
default:
return 0;
}
}

You might also like