Roman numerals/Encode: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Java example)
m (→‎{{header|Java}}: Changed to long ints, shortened a few lines)
Line 80: Line 80:
System.out.println(roman(954));
System.out.println(roman(954));
}
}
public static String roman(int n){
public static String roman(long n){
String result = "";
String result = "";
if(n >= 1000){
if(n >= 1000){
result+= (copies("M",(n / 1000)));
result+= (copies("M",(n / 1000)));
n= n % 1000;
n%= 1000;
}
}
if(n >= 900){
if(n >= 900){
result+= "CM";
result+= "CM";
n= n % 900;
n%= 900;
}
}
if(n >= 500){
if(n >= 500){
result+= "D";
result+= "D";
n= n % 500;
n%= 500;
}
}
if(n >= 100){
if(n >= 100){
result+= (copies("C",(n / 100)));
result+= (copies("C",(n / 100)));
n= n % 100;
n%= 100;
}
}
if(n >= 90){
if(n >= 90){
result+= "XC";
result+= "XC";
n= n % 90;
n%= 90;
}
}
if(n >= 50){
if(n >= 50){
result+= "L";
result+= "L";
n= n % 50;
n%= 50;
}
}
if(n >= 40){
if(n >= 40){
result+= "XL";
result+= "XL";
n= n % 40;
n%= 40;
}
}
if(n >= 10){
if(n >= 10){
result+= (copies("X",(n / 10)));
result+= (copies("X",(n / 10)));
n= n % 10;
n%= 10;
}
}
if(n== 9){
if(n== 9){
Line 120: Line 120:
if(n >= 5){
if(n >= 5){
result+= "V";
result+= "V";
n= n % 5;
n%= 5;
}
}
if(n== 4){
if(n== 4){
Line 132: Line 132:
public static String copies(String a, int n){
public static String copies(String a, int n){
String result = "";
String result = "";
for(int i=0;i<n;i++,result+=a);
for(int i= 0;i < n;i++,result+= a);
return result;
return result;
}
}

Revision as of 02:04, 1 April 2008

Task
Roman numerals/Encode
You are encouraged to solve this task according to the task description, using any language you may know.

Create a function taking a positive integer as its parameter and returning a string containing the Roman Numeral representation of that integer.

Ada

<Ada> with Ada.Text_Io; use Ada.Text_Io; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

procedure Roman_Numeral_Test is

  function To_Roman(Number : Positive) return String is
     Result : Unbounded_String;
     Current_Value : Natural := Number;
  begin
     if Current_Value >= 1000 then
        Result := Result & ((Current_Value / 1000) * 'M');
        Current_Value := Current_Value mod 1000;
     end if;
     if Current_Value >= 900 then
        Result := Result & "CM";
        Current_Value := Current_Value mod 900;
     end if;
     if Current_Value >= 500 then
        Result := Result & "D";
        Current_Value := Current_Value mod 500;
     end if;
     if Current_Value >= 100 then
        Result := Result & ((Current_Value / 100) * 'C');
        Current_Value := Current_Value mod 100;
     end if;
     if Current_Value >= 90 then
        Result := Result & "XC";
        Current_Value := Current_Value mod 90;
     end if;
     if Current_Value >= 50 then
        Result := Result & "L";
        Current_Value := Current_Value mod 50;
     end if;
     if Current_Value >= 40 then
        Result := Result & "XL";
        Current_Value := Current_Value mod 40;
     end if;
     if Current_Value >= 10 then
        Result := Result & ((Current_Value / 10) * 'X');
        Current_Value := Current_Value mod 10;
     end if;
     if Current_Value = 9 then
        Result := Result & "IX";
        Current_Value := 0;
     end if;
     if Current_Value >= 5 then
        Result := Result & "V";
        Current_Value := Current_Value mod 5;
     end if;
     if Current_Value = 4 then
        Result := Result & "IV";
        Current_Value := 0;
     end if;
     Result := Result & (Current_Value * 'I');
     return To_String(Result);
  end To_Roman;

begin

  Put_Line(To_Roman(1999));
  Put_Line(To_Roman(25));
  Put_Line(To_Roman(944));

end Roman_Numeral_Test; </Ada> Output:

MCMXCIX
XXV
CMXLIV

Java

Translation of: Ada

The helper function copies is added since Java does not support String multiplication. <java>public class RN{ public static void main(String args[]){ System.out.println(roman(1999)); System.out.println(roman(25)); System.out.println(roman(954)); } public static String roman(long n){ String result = ""; if(n >= 1000){ result+= (copies("M",(n / 1000))); n%= 1000; } if(n >= 900){ result+= "CM"; n%= 900; } if(n >= 500){ result+= "D"; n%= 500; } if(n >= 100){ result+= (copies("C",(n / 100))); n%= 100; } if(n >= 90){ result+= "XC"; n%= 90; } if(n >= 50){ result+= "L"; n%= 50; } if(n >= 40){ result+= "XL"; n%= 40; } if(n >= 10){ result+= (copies("X",(n / 10))); n%= 10; } if(n== 9){ result+= "IX"; n= 0; } if(n >= 5){ result+= "V"; n%= 5; } if(n== 4){ result+= "IV"; n= 0; } result+= (copies("I",n)); return result; }

public static String copies(String a, int n){ String result = ""; for(int i= 0;i < n;i++,result+= a); return result; } }</java> Output:

MCMXCIX
XXV
CMXLIV