I have just recently begun programming, and am learning Pascal as my first language. In my class, the teacher doesn't have huge amounts of time, so I thought I would research this online. Converting arabic (normal) numbers to roman numerals is a part of a project that we are working on. I can do it, but I used a couple hundred lines of code to do it.
Now, just to make sure, I'm not asking for the code to be laid out. I know sort of how to do it; it involves a loop, but I'm not sure exactly how. Wrapping my head around it is a little tricky; at least at the moment. Given my beginner status, I would however appreciate simple English if you wish to offer some advice, or a pointer in the right direction. In case I don't end up getting back to you, my sincere thanks is offered.

debugger

Dani AI

Generated

A compact, beginner-friendly way to think about this: build a single ordered table of Roman-symbol/value pairs (include the subtractive forms) and process the number from largest to smallest. For each pair use integer division (or divmod) to find how many times the symbol fits, append that many symbols, then replace the working number with the remainder. That "largest-to-smallest, take-as-many-as-you-can" (greedy) approach does the same job as breaking the number into thousands/hundreds/tens/units (as did) but keeps the logic in one short loop instead of many case blocks. Note that standard modern notation commonly covers 1 through 3999; historic and clockface variants exist. Roman numerals (Wikipedia). (en.wikipedia.org)

A minimal Python-style template (mapping omitted for brevity) shows the structure you need:

def int_to_roman(n):
    table = [(value, symbol), ...]  # highest-first; include subtractive pairs
    parts = []
    for value, symbol in table:
        q, n = divmod(n, value)
        if q:
            parts.append(symbol * q)
    return ''.join(parts)

Using divmod gives quotient and remainder in one call (equivalent to (n // value, n % value) for integers). divmod — Python built-ins. (docs.python.org)

Practical notes: validate input (no zero/negatives unless you choose a convention), decide how to handle values outside the conventional range, and write small tests for edge cases (4, 9, 40, 90, 400, 900). For Pascal learners, the same pattern applies using Div and Mod inside a single loop rather than many separate case statements; this typically cuts a multi-hundred-line program down to a handful of readable lines.

Recommended Answers

All 4 Replies

read this forum thread

Yes, I did actually see that site; however that was the main reason that I asked for simple English - I understand some of it, but I suspect that most of it is in pascal jargon. I didn't understand, and so I asked here.
However, thank you very much for putting it up; I am very grateful.

The parts that I didn't really understand were what exactly to do with the numbers - having a percentage sign between the two doesn't mean anything to me. Nor does the phrasing of how to do it.
I am sure, however, that it might well be exactly what someone with a bit more knowledge in this subject / subject area needs.
Again, thank you.

you have to use the 'Div' and the 'Mod' commands
like
33 Div 2 = 16
33 Mod 2 = 1

I created a solution :D

(*
Roman Numbers
1=I,2=II,3=III,4=IV,5=V,6=VI,7=VII,8=VIII,9=IX,10=X
11=XI,12=XII,13=XIII,14=XIV,15=XV,16=XVI,17=XVII
18=XVIII,19=XIX,
20=XX,30=XXX,40=XL,50=L,60=LX,70=LXX,80=LXXX,90=XC
100=C,200=CC,300=CCC,400=CD,500=D,600=DC,700=DCC,800=DCCC
900=CM,
1000=M,2000=MM


First, we take the number and divide by the most
highest Roman value we can have. In this case
the most highest value is 1000 which is represented
by "M" on Roman numeric system.
EXAMPLE:
1945 Div 1000 = 1 then 1945 Mod 1000 = 945
945 Div 100 = 9 then 945 Mod 100 = 45
45 Div 10 =4 then 45 Mod 10 = 5
add the last remainder to the final result.

in pascal
*)
Program Solution01;

Uses Crt;

Var User,Temp,D,i:Integer;
    res:String;


Begin
   res:='';
   ClrScr;

   Write('Give me a number between 1 and 3000: ');

   {$I-}
   ReadLn(User);
   {$I+}

   {if the user pressed a char insted of integer value}
   {or enter to big number then the program will exit}
   If (IoResult<>0)Or(User>=3000)Then Begin
      Write('Wrong value');
      Readkey;
      Halt;
   End;

   {everything is ok then let's see the algorithm}
   Temp:=User;

   Repeat
     {check the thousands}
     If (Temp Div 1000 <> 0)Then Begin
        D:=Temp Div 1000;
        For i:=1 To D Do Begin
           res:=res+'M';
        End;
        Temp:= Temp Mod 1000;
     End
     Else Temp:= Temp Mod 1000;

     {check the hundreds}
     If (Temp Div 100 <> 0)Then Begin
        D:=Temp Div 100;
        Case (D) Of
           1:res:=res+'C';
           2:res:=res+'CC';
           3:res:=res+'CCC';
           4:res:=res+'CD';
           5:res:=res+'D';
           6:res:=res+'DC';
           7:res:=res+'DCC';
           8:res:=res+'DCCC';
           9:res:=res+'CM';
        End;
        Temp:=Temp Mod 100;
     End;

     {check the decimals}
     If (Temp Div 10 <> 0) Then Begin
       D:=Temp Div 10;
       Case (D) Of
           1:res:=res+'X';
           2:res:=res+'XX';
           3:res:=res+'XXX';
           4:res:=res+'XL';
           5:res:=res+'L';
           6:res:=res+'LX';
           7:res:=res+'LXX';
           8:res:=res+'LXXX';
           9:res:=res+'XC';
        End;
        Temp:=Temp Mod 10;
     End;

     {and check the numbers from 1 to 9}
     Case (Temp) Of
         1:res:=res+'I';
         2:res:=res+'II';
         3:res:=res+'III';
         4:res:=res+'IV';
         5:res:=res+'V';
         6:res:=res+'VI';
         7:res:=res+'VII';
         8:res:=res+'VIII';
         9:res:=res+'IX';
      End;

   Until Temp <= 10;

   {write the results to the screen}
   Write(User,'=',res);

   ReadLn;
End.

{-=Created By FlamingClaw 2009.05.13=-}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.