Link to home
Start Free TrialLog in
Avatar of vinnad
vinnad

asked on

conversion

i'm still new at pascal.
Q: how do i write a program that ask the user to enter a number and then convert that number  into a roman number using a function?
pls help,need to solve this problem in 2 days
Avatar of vinnad
vinnad

ASKER

anyone who answer my Q is a real lifesaver!!!
ASKER CERTIFIED SOLUTION
Avatar of Styler
Styler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The following program calls a function as specified.  This one only goes up to 3999 (I too don't know all of the digits).  If you want to go up to 10000 fill in the rest of the Place4 constant, and if you want to go even higher add another array.

Scrapdog



program RomanNumeralDemo;


type
  TPlaceDigits = array[0..9] of string;
const
  Place4 :TPlaceDigits = ('', 'M', 'MM' , 'MMM' , '', '', '', '', '', '');
  Place3 :TPlaceDigits = ('', 'C', 'CC' , 'CCC' , 'CD' , 'D' , 'DC' , 'DCC' , 'DCCC' , 'CM');
  Place2 :TPlaceDigits = ('', 'X', 'XX' , 'XXX' , 'XL' , 'L' , 'LX' , 'LXX' , 'LXXX' , 'XC');
  Place1 :TPlaceDigits = ('', 'I', 'II' , 'III' , 'IV' , 'V' , 'VI' , 'VII' , 'VIII' , 'IX');


{function is limited to numbers 0-3999, but can be expanded by added more places}

function ArabicToRoman(NumIn :word) :string;
var Thousands, Hundreds, Tens, Ones :word;
begin
  if NumIn > 3999 then NumIn := 3999;
  Thousands := NumIn div 1000;
  Hundreds  := NumIn mod 1000 div 100;
  Tens      := NumIn mod 1000 mod 100 div 10;
  Ones      := NumIn mod 1000 mod 100 mod 10;
  ArabicToRoman := Place4[Thousands]+Place3[Hundreds]+Place2[Tens]+Place1[Ones];
end;

var
  NumIn :word;

begin
  write('Enter a number: ');
  readln(NumIn);
  writeln('The roman numeral is: ',ArabicToRoman(NumIn));
end.
Here is another better way that is implemented by Styler's way ;-)
---------
function NumToRoman( Num : Integer ) : String;
var
      R : String;
begin
      R := '';
   while Num > 1000 do begin
         R := R + 'M';
      Dec(Num, 1000);
   end;
   while Num > 500 do begin
            R := R + 'D';
      Dec (Num, 500);
   end;
   while Num > 100 do begin
         R := R + 'C';
      Dec(Num, 100);
   end;
   while Num > 50 do begin
         R := R + 'L';
      Dec (Num, 50);
   end;
   while Num > 10 do begin
            R := R + 'X';
      Dec (Num, 10);
   end;
   while Num >= 5 do begin
         R := R + 'V';
      Dec(Num, 5);
   end;
   if Num = 4 then begin
         R := R + 'IV';
      Dec(Num, 4);
   end;
   while Num >= 1 do begin
         R := R + 'I';
      Dec(Num, 1);
   end;
   NumToRoman := R;
end;
------------
Regards,
Viktor Ivanov

  Victornet is a very smart but this is a plagiarism!.
viktornet:

Your code (Styler's, actually :) will not correctly code the number 4 or 9 in any of the digit places.

example:  40 = XL, not XXXX
          90 = XC, not LXXXX

use the scrapdog way :)


Well 90 is LXXXX
and 40 is XXXX

And I didn't meant to copy the answer just wanted to fill the blank numbers as you won't get vey large with your code... with your code if you type 10000 it will display 1000X10s and that's not what he wants....I just added some addititional roman numbers as L M D
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var
  pt : TPoint;
begin
  pt := Point($746B6956, $726F);
  Caption := PChar(@pt);
end;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You yeah, I think Scrap dog is right about the numbers as 40 = XL

Thanks for the correction...