Yeah... what language?
in java:
Code:
private static String RN(int number){
int[] nums = { 1000, 500, 100, 50, 10, 5, 4, 1};
String[] numerals = { "M", "D", "C", "L", "X", "V", "IV", "I"};
boolean done = false; int x = 0;
while(done == false && x < 8)
{
if(number >= nums[x]){
done = true;
return (numerals[x] + RN(number - nums[x]));
}
x++;
}
return "";
}
private static int number(String RN){
int[] vals = new int[RN.length()];
int total = 0;
for(int x = 0; x < RN.length(); x++)
vals[x] = val(RN.charAt(x));
for(int x = 0; x < RN.length() - 1; x++){
if(vals[x] < vals[x+1])
total -= vals[x];
else
total += vals[x];
}
total += vals[RN.length() - 1];
return total;
}
private static int val(char c){
int[] nums = { 1000, 500, 100, 50, 10, 5, 1};
char[] numerals = { 'M', 'D', 'C', 'L', 'X', 'V', 'I'};
for(int x = 0; x < 7; x++){
if(c == numerals[x])
return nums[x];
}
return 0;
}