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


#define ZEROC asc("0")
 
function is_num( byval c as string ) as boolean
    if asc(c) >= ZEROC andalso asc(c)<ZEROC+10 then return true
    return false
end function
 
function is_good_isbn( isbn as string ) as boolean
    dim as uinteger charno = 0, digitno = 0, sum = 0
    dim as string*1 currchar
    while charno <= len(isbn)
        currchar = mid(isbn,charno,1)
        if is_num(currchar) then
            if digitno mod 2 = 1 then
                sum += 2*(asc(currchar)-ZEROC)
            end if
            sum += asc(currchar)-ZEROC
            digitno += 1
        end if
        charno += 1
    wend
    if sum mod 10 = 0 then 
        return true
    else 
        return false
    end if
end function
 
dim as string isbns(0 to 3) = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }
dim as uinteger i
for i = 0 to 3
    if is_good_isbn( isbns(i) ) then
        print isbns(i)+": good"
    else
        print isbns(i)+": bad"
    end if
next i