Demonstrates a number trick. Enter a number and the program reverses its digits, prints the difference between the number and its reverse, then reverses that difference and adds the two together. With most three-digit numbers whose first and last digits differ, the final total is always 1089.
Program Analysis
The program is about twenty lines. The number is taken as a string so that it can be reversed character by character (lines 50–70), and VAL turns both forms back into numbers. The positive difference is worked out with two IF tests, turned into a string with STR$, reversed the same way and added to itself. PAUSE 40000 waits for a key before the program restarts with RUN.
Potential Issues
- If the number reads the same in both directions, neither
IFat lines 110–120 setsC, so line 130 stops with an error on the first run and prints the previous value later. - A two-digit difference such as 99 is reversed as “99” and not “990”, because the program does not pad it to three digits. Such numbers give 198 in place of 1089.
Source Code
10 PRINT "ENTER A NUMBER?"
20 INPUT A$
30 LET A=VAL A$
40 LET B$=""
50 FOR N=LEN A$ TO 1 STEP -1
60 LET B$=B$+A$(N)
70 NEXT N
80 PRINT ,,,,"DIFFERENCE BETWEEN ";A$
90 PRINT "AND ";B$;" IS:"
100 LET B=VAL B$
110 IF (A-B)>0 THEN LET C=A-B
120 IF (B-A)>0 THEN LET C=B-A
130 PRINT C
140 LET C$=STR$ C
150 LET D$=""
160 FOR N=LEN C$ TO 1 STEP -1
170 LET D$=D$+C$(N)
180 NEXT N
190 LET D=VAL D$
200 PRINT ,,C;" + ";D;" = ";C+D
210 PAUSE 40000
220 CLS
230 RUN
9998 SAVE "ENIGM[A]"
9999 RUNNote: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
