如何用 Pascal 寫 Roman Numerals

請用 Pascal 寫

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

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    program assignment1c (input, output);

    var number : integer;

    charnum1 : integer;

    charnum2 : integer;

    charnum3 : integer;

    charnum4 : integer;

    outstr : string;

    begin

    outstr := ' ';

    repeat

    write('please input a number : ');

    readln(number);

    if (number < 1) or (number > 2500) then

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

    until (number >= 1) and (number <= 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 + 'M';

    2 : outstr := outstr + 'MM';

    end;

    case charnum2 of

    1 : outstr := outstr + 'C';

    2 : outstr := outstr + 'CC';

    3 : outstr := outstr + 'CCC';

    4 : outstr := outstr + 'CD';

    5 : outstr := outstr + 'D';

    6 : outstr := outstr + 'DC';

    7 : outstr := outstr + 'DCC';

    8 : outstr := outstr + 'DCCC';

    9 : outstr := outstr + 'CM';

    end;

    case charnum3 of

    1 : outstr := outstr + 'X';

    2 : outstr := outstr + 'XX';

    3 : outstr := outstr + 'XXX';

    4 : outstr := outstr + 'XL';

    5 : outstr := outstr + 'L';

    6 : outstr := outstr + 'LX';

    7 : outstr := outstr + 'LXX';

    8 : outstr := outstr + 'LXXX';

    9 : outstr := outstr + 'XC';

    end;

    case charnum4 of

    1 : outstr := outstr + 'I';

    2 : outstr := outstr + 'II';

    3 : outstr := outstr + 'III';

    4 : outstr := outstr + 'IV';

    5 : outstr := outstr + 'V';

    6 : outstr := outstr + 'VI';

    7 : outstr := outstr + 'VII';

    8 : outstr := outstr + 'VIII';

    9 : outstr := outstr + 'IX';

    end;

    writeln('The roman numeral of ', number, ' is : ', outstr)

    end.

Still have questions? Get your answers by asking now.