# 如何用 Pascal 寫 Roman Numerals

Write a program to show the roman numerals of an input number between 1 and 2500. ( where 1:I , 5:V , 10:X , 50:L , 100:C , 500:D , 1000:M )

Hint : use DIV and MOD

Rating

program assignment1c (input, output);

var number : integer;

charnum1 : integer;

charnum2 : integer;

charnum3 : integer;

charnum4 : integer;

outstr : string;

begin

outstr := &#39; &#39;;

repeat

write(&#39;please input a number : &#39;);

if (number &lt; 1) or (number &gt; 2500) then

writeln(&#39;number must be between 1 to 2500, please try again!&#39;);

until (number &gt;= 1) and (number &lt;= 2500);

charnum1 := number div 1000;

charnum2 := (number div 100) mod 10;

charnum3 := (number div 10) mod 10;

charnum4 := number mod 10;

case charnum1 of

1 : outstr := outstr + &#39;M&#39;;

2 : outstr := outstr + &#39;MM&#39;;

end;

case charnum2 of

1 : outstr := outstr + &#39;C&#39;;

2 : outstr := outstr + &#39;CC&#39;;

3 : outstr := outstr + &#39;CCC&#39;;

4 : outstr := outstr + &#39;CD&#39;;

5 : outstr := outstr + &#39;D&#39;;

6 : outstr := outstr + &#39;DC&#39;;

7 : outstr := outstr + &#39;DCC&#39;;

8 : outstr := outstr + &#39;DCCC&#39;;

9 : outstr := outstr + &#39;CM&#39;;

end;

case charnum3 of

1 : outstr := outstr + &#39;X&#39;;

2 : outstr := outstr + &#39;XX&#39;;

3 : outstr := outstr + &#39;XXX&#39;;

4 : outstr := outstr + &#39;XL&#39;;

5 : outstr := outstr + &#39;L&#39;;

6 : outstr := outstr + &#39;LX&#39;;

7 : outstr := outstr + &#39;LXX&#39;;

8 : outstr := outstr + &#39;LXXX&#39;;

9 : outstr := outstr + &#39;XC&#39;;

end;

case charnum4 of

1 : outstr := outstr + &#39;I&#39;;

2 : outstr := outstr + &#39;II&#39;;

3 : outstr := outstr + &#39;III&#39;;

4 : outstr := outstr + &#39;IV&#39;;

5 : outstr := outstr + &#39;V&#39;;

6 : outstr := outstr + &#39;VI&#39;;

7 : outstr := outstr + &#39;VII&#39;;

8 : outstr := outstr + &#39;VIII&#39;;

9 : outstr := outstr + &#39;IX&#39;;

end;

writeln(&#39;The roman numeral of &#39;, number, &#39; is : &#39;, outstr)

end.