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


get "libhdr"
 
let checkISBN(s) = valof
$(  let tally = 0
    unless s%0 = 14 resultis false
    unless s%4 = '-' resultis false 
 
    for i=1 to 3
    $(  let digit = s%i-'0'
        test i rem 2 = 0 
            do tally := tally + 3 * digit
            or tally := tally + digit
    $)
 
    for i=5 to 14
    $(  let digit = s%i-'0'
        test i rem 2 = 0
            do tally := tally + digit
            or tally := tally + 3 * digit
    $)
 
    resultis tally rem 10 = 0
$)
 
let show(s) be
    writef("%S: %S*N", s, checkISBN(s) -> "good", "bad")
 
let start() be
$(  show("978-1734314502")
    show("978-1734314509")
    show("978-1788399081")
    show("978-1788399083")
$)