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


BEGIN # Check some IsBN13 check digits                                         #
    # returns TRUE if the alledged isbn13 has the correct check sum,           #
    #         FALSE otherwise                                                  #
    #         non-digit characters are ignored and there must be 13 digits     #
    PROC check isbn13 = ( STRING isbn13 )BOOL:
         BEGIN
            INT sum          := 0;
            INT digits       := 0;
            BOOL other digit := FALSE;
            FOR pos FROM LWB isbn13 TO UPB isbn13 DO
                IF CHAR c = isbn13[ pos ];
                   c >= "0" AND c <= "9"
                THEN
                    # have another digit                                       #
                    digits +:= 1;
                    sum    +:= ( ABS c - ABS "0" ) * IF other digit THEN 3 ELSE 1 FI;
                    other digit := NOT other digit
                FI
            OD;
            digits = 13 AND sum MOD 10 = 0
         END; # check isbn13 #
    # task test cases #
    []STRING tests    = ( "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" );
    []BOOL   expected = (             TRUE,            FALSE,             TRUE,            FALSE );
    FOR pos FROM LWB tests TO UPB tests DO
        BOOL result = check isbn13( tests[ pos ] );
        print( ( tests[ pos ]
               , ": "
               , IF result THEN "good" ELSE "bad" FI
               , IF result = expected[ pos ] THEN "" ELSE " NOT AS EXPECTED" FI
               , newline
               )
             )
    OD
END