Roman numbers converter in BASIC

Written April 11th, 2020 by

To pass the time I thought to brush up the programming in BASIC a little by writing a simple program for converting from decimal (or Arabic) to Roman numbers and vice versa. The program is portable enough to run on ZX Spectrum, Commodore 64, MSX home computers and on CP/M systems equipped with MBASIC interpreter.

Roman notation differs from ours in the use of letters instead of numbers. The following table lists the Roman symbols and the corresponding decimal values.

I 1
V 5
X 10
L 50
C 100
D 500
M 1000

The program asks to enter a number, either decimal or Roman, and converts it to the corresponding Roman or decimal notation, respectively.

Commodore 64

MSX

ZX Spectrum

Listing

The following listing is compatible with the BASIC interpreters of MSX, Commodore 64 and CP/M computers.

10 DATA 1000,"M"
11 DATA 900,"CM"
12 DATA 500,"D"
13 DATA 400,"CD"
14 DATA 100,"C"
15 DATA 90,"XC"
16 DATA 50,"L"
17 DATA 40,"XL"
18 DATA 10,"X"
19 DATA 9,"IX"
20 DATA 5,"V"
21 DATA 4,"IV"
22 DATA 1,"I"
23 DATA 0,""
30 A$="" : INPUT "Numero ";A$
35 IF A$="" THEN GOTO 300
40 RESTORE
45 IF LEFT$(A$,1)>="A" AND LEFT$(A$,1)<="Z" THEN GOTO 200
100 REM Da decimale a romano
105 N=VAL(A$) : R$=""
110 PRINT N;"= ";
115 READ V,L$
120 IF V=0 THEN GOTO 150
125 IF N<V THEN GOTO 115
130 N=N-V
135 R$=R$+L$
140 GOTO 125
150 PRINT R$
155 GOTO 30
200 REM Da romano a decimale
205 R=0
210 PRINT A$;" =";
215 READ V,L$
220 IF V=0 THEN GOTO 250
225 IF LEN(A$)<LEN(L$) THEN GOTO 215
230 IF LEFT$(A$,LEN(L$))<>L$ THEN GOTO 215
235 R=R+V
240 A$=RIGHT$(A$,LEN(A$)-LEN(L$))
245 GOTO 225
250 PRINT R
255 GOTO 30
300 REM End

To work on the ZX Spectrum it is necessary to modify some lines as the interpreter does not provide the RIGHT$ and LEFT$ functions for strings manipulation.

10 DATA 1000,"M"
11 DATA 900,"CM"
12 DATA 500,"D"
13 DATA 400,"CD"
14 DATA 100,"C"
15 DATA 90,"XC"
16 DATA 50,"L"
17 DATA 40,"XL"
18 DATA 10,"X"
19 DATA 9,"IX"
20 DATA 5,"V"
21 DATA 4,"IV"
22 DATA 1,"I"
23 DATA 0,""
30 LET a$="": INPUT "Numero "; LINE a$
35 IF a$="" THEN GO TO 300
40 RESTORE 
45 IF a$(1)>="A" AND a$(1)<="Z" THEN GO TO 200
100 REM Da decimale a romano
105 LET n=VAL (a$): LET r$=""
110 PRINT n;" = ";
115 READ v,l$
120 IF v=0 THEN GO TO 150
125 IF n<v THEN GO TO 115
130 LET n=n-v
135 LET r$=r$+l$
140 GO TO 125
150 PRINT r$
155 GO TO 30
200 REM Da romano a decimale
205 LET r=0
210 PRINT a$;" = ";
215 READ v,l$
220 IF v=0 THEN GO TO 250
225 IF LEN (a$)<LEN (l$) THEN GO TO 215
230 IF a$(1 TO LEN (l$))<>l$ THEN GO TO 215
235 LET r=r+v
240 LET a$=a$(LEN (l$)+1 TO LEN (a$))
245 GO TO 225
250 PRINT r
255 GO TO 30
300 REM End

The program is very simple and does not perform many checks, so it is necessary to follow some precautions, such as not converting numbers greater than 3999 and using capital letters for Roman numbers.

For a more complete discussion of Roman notation, I refer to the corresponding article on Wikipedia.

Files