Files
gw-basic-2026/tests/programs/luhn.bas
Eremey Valetov 691031a7f9 Fix RESTORE with line number, add 8 Rosetta Code test programs
RESTORE n now correctly positions past the DATA token so the next
READ gets actual values instead of the token byte.

New tests: Roman numerals, Luhn validator, Towers of Hanoi, 100 Doors,
Pascal's triangle, type declarations, Hailstone sequence, multiplication
table.  50 tests now pass.
2026-02-15 16:22:55 -05:00

22 lines
584 B
QBasic

10 REM Luhn algorithm - credit card validator - Rosetta Code
20 DATA "49927398716", 1, "49927398717", 0
30 DATA "1234567812345670", 1, "1234567812345678", 0
40 FOR T = 1 TO 4
50 READ CARD$
60 S = 0
70 NDIG = LEN(CARD$)
80 PARITY = NDIG MOD 2
90 FOR I = 1 TO NDIG
100 D = VAL(MID$(CARD$, I, 1))
110 IF ((I-1) MOD 2) <> PARITY THEN 140
120 D = D * 2
130 IF D > 9 THEN D = D - 9
140 S = S + D
150 NEXT I
160 RESULT = 0
170 IF (S MOD 10) = 0 THEN RESULT = 1
180 READ EXPECTED
190 IF RESULT <> EXPECTED THEN PRINT "FAIL: "; CARD$; " sum="; S : END
200 NEXT T
210 PRINT "Luhn OK"