Task

Validate the check digit of an ISBN-13 code:

   Multiply every other digit by  3.
   Add these numbers and the other digits.
   Take the remainder of this number after division by  10.
   If it is  0,   the ISBN-13 check digit is correct.


Use the following codes for testing:

   978-1734314502       (good)
   978-1734314509         (bad)
   978-1788399081       (good)
   978-1788399083         (bad)


Show output here, on this page



See also

   for details:   13-digit ISBN method of validation.       (installs cookies.)


100 PROGRAM "ISBN13.bas"
110 DO
120   PRINT :INPUT PROMPT "ISBN-13 code: ":IS$
130   IF IS$="" THEN EXIT DO
140   IF ISBN(IS$) THEN
150     PRINT "Ok."
160   ELSE
170     PRINT "CRC error."
180   END IF
190 LOOP
200 DEF TRIM$(S$)
210   LET T$=""
220   FOR I=1 TO LEN(S$)
230     IF S$(I)>CHR$(47) AND S$(I)<CHR$(58) THEN LET T$=T$&S$(I)
240   NEXT
250   LET TRIM$=T$
260 END DEF
270 DEF ISBN(S$)
280   LET SUM,ISBN=0:LET ISBN$=TRIM$(IS$)
290   IF LEN(ISBN$)<>13 THEN PRINT "Invalid length.":EXIT DEF
300   FOR I=1 TO 11 STEP 2
310     LET SUM=SUM+VAL(ISBN$(I))+VAL(ISBN$(I+1))*3
320   NEXT
330   LET SUM=SUM+VAL(ISBN$(13))
340   IF MOD(SUM,10)=0 THEN LET ISBN=-1
350 END DEF