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.)


100H:
 
CHECK$ISBN13: PROCEDURE (PTR) BYTE;
    DECLARE PTR ADDRESS, ISBN BASED PTR BYTE;
    DECLARE (I, F, T) BYTE;
    F = 1;
    T = 0;
    DO I = 0 TO 13;
        IF I = 3 THEN DO;
            /* THIRD CHAR SHOULD BE '-' */
            IF ISBN(I) <> '-' THEN RETURN 0;
        END;
        ELSE DO;
            /* DIGITS MUST BE VALID */
            IF ISBN(I) < '0' OR ISBN(I) > '9' THEN RETURN 0;
            T = T + (ISBN(I) - '0') * F;
            F = 4 - F; /* MULTIPLY BY 1 AND 3 ALTERNATELY */
        END;
    END;
    RETURN (T MOD 10) = 0;
END CHECK$ISBN13;
 
/* CP/M BDOS CALL */
BDOS: PROCEDURE (FUNC, ARG);
    DECLARE FUNC BYTE, ARG ADDRESS;
    GO TO 5;
END BDOS;
 
PRINT: PROCEDURE (STR);
    DECLARE STR ADDRESS;
    CALL BDOS(9, STR);
END PRINT;
 
/* TESTS */
DECLARE TEST (4) ADDRESS;
TEST(0) = .'978-1734314502$';
TEST(1) = .'978-1734314509$';
TEST(2) = .'978-1788399081$';
TEST(3) = .'978-1788399083$';
 
DECLARE I BYTE;
DO I = 0 TO LAST(TEST);
    CALL PRINT(TEST(I));
    CALL PRINT(.': $');
    IF CHECK$ISBN13(TEST(I)) THEN
        CALL PRINT(.'GOOD$');
    ELSE
        CALL PRINT(.'BAD$');
    CALL PRINT(.(13,10,'$'));
END;
 
CALL BDOS(0,0);
EOF