Classic Computer Magazine Archive COMPUTE! ISSUE 26 / JULY 1982 / PAGE 96

Multidigit Addition

Zoltan Szepesi
Pittsburgh

For Commodore computers, this program allows you to add very large numbers together.

A microcomputer generally cannot handle more than 8 to 12 digits of precision. However, special programs can be written in BASIC or machine language to increase the precision of the calculations.

In this program, Multadd, the addition is made in eight-digit groups. The two numbers to be added together (the addenda) are entered as two strings between lines 70 to 120, using a modified version of Gary Greenberg's simulated input routine (COMPUTE!, May/June, 1980, #4). This way, up to 254 digits can be entered for each number. The two addenda can have different numbers of digits. Lines 140 to 160 make them equal in length by placing zeros in front of the shorter number.

Lines 170 to 250 divide the strings into eight-digit groups and transform these substrings into numeric values. Then lines 260 to 300 do the addition, and 310 to 380 reconvert the groups to eight-digit strings, taking care to fill up the empty places with zeros, and eliminating the spacing between the groups. The result is printed out as a continuous string.

10 PRINT"{CLEAR}           MULTIDIGIT ADDITION"
40 PRINT" {DOWN} THE NUMBERS TO BE ADDED CAN HAVE MAX."
50 PRINT" 254 DIGITS.TYPE THE DIGITS CONTINUALLY, THEN PRESS ‘RETURN’."
60 PRINT"CORRECT WRONG NUMBER WITH ‘DEL’."
70 PRINT" {DOWN}     FIRST ADDENDUM : A = " : PRINT" {RIGHT}";
80 GOSUB 500
90 A$ = "0" + Z1$
100 PRINT : PRINT"    SECOND ADDENDUM : B = ": PRINT" {RIGHT}";
110 GOSUB 500
120 B$ ="0" + Z1$
130 T1 = TI : PRINT : PRINT"   {REV} WORKING"
140 LA = LEN(A$): LB = LEN (B$): D$ = ""
150 IF   LB<LP  THEN LD = LA-LB: FOR   I = 1   TO   LD: D$ = D$ + "0" : NEXT I : B$ = D$ + B$
160 IF LA<LB THEN LD = LB-LA : FOR I = 1 TO LD : D$ = D$ + "0" : NEXT I : A$ = D$ + A$ : LA = LEN (A$)
170 M = INT (LA/8) : Q = LA-8*M : IF Q>0 THEN M = M + 1
180 DIM A(M), B(M), C(M), D(M), R(M), R$(M), F$(M)
190 FOR I = 0 TO M : D(I) = 0 : C(I) = 0 : R(I) = 0 : NEXT ~ I
200 IF M = l THEN A(1) = VAL(A$): B(1) = VAL(B$) : GOTO 260
210 IF Q>0 THEN A(1) = VAL (LEFT $ (A$, Q)) : B (1) =  VAL(LEFT $ (B$, Q)): X = l : GOTO 230
220 A(l) = VAL (LEFT$ (A$, 8)) : B(1) = VAL (LEFT$(B$, 8)): X = 0
230 FOR I = 2 TO M
240 A (I) = VAL (MID$ (A$,(I-l-X) *8+l + Q ,8)) : B(I)= VAL (MID$ (B$, (I-l-X) *8 + l + Q, 8))
250 NEXT I
260 FOR I = M TO 1 STEP -1
270 C(I) = A (I) + B (I) + D(I)
280 D(I-l) = INT (C(I)/1E8)
290 R(I) = C(I)-D(I-l) * 1E8
300 NEXT I
310 PRINT : PRINT"  THE SUM : A + B =": R$(1) = STR $(R(l)) : IF R(1) = 0 THENR $(1) ="{RIGHT}"

320 PRINT R$ (1); : IFM = 1 GOTO 390
330 FOR I = 2 TO M : R$ (I) = STR $ (R(I))
340 P = LEN(R$(I)) : R$(I) = RIGHT $ (R$(I), P-1) : F$ (1)  = ""
350 FOR J = 8 TO 2 STEP -1
360 IF P< = J THEN F$(I) = F$(I) + "0" : NEXT J
370 R $(I) = F$ (I) + R$(I)
380 PRINT R$(I); : NEXT I : PRINT
390 PRINT : PRINT" EXECUTION TIME:"; INT ((TI-Tl)*100/60+.5)/100;"SEC"
400 PRINT : PRINT"DO YOU WANT TO CONTINUE? (Y OR N) "
410 GET V$ :IF V$ = ""GOTO 410
420 IF V$ = "Y" THEN CLR : PRINT"{CLEAR} {DOWN}" : GOTO 70
430 IF V$<>"N" GOTO 400
440 IF V$ = "N" GOTO 620
500 Z$ = "" : Z1$ = ""
510 PRINT "$ {LEFT}";
520 GET Z$ : IF Z$ = "" GOTO 510
530 PRINT " {LEFT}";
540 IF Z$ <> CHR$ (20) GOTO 580
550 IF Z$ = "" GOTO 510
560 ZZ = LEN (Z1$) : IF ZZ<1 GOTO 510
570 Z1$ = LEFT$ (Z1$, ZZ-1) : PRINT "{LEFT}"; : GOTO 510
580 IF Z $ = CHR$ (13) OR Z$ = CHR$ (141) GOTO 610
590 PRINT Z$;
600 Z1$ = Z1$ + Z$ : GOTO 510
610 RETURN
620 END