RosettaCodeData/Task/ISBN13-check-digit/PascalABC.NET/isbn13-check-digit.pas
2025-02-27 18:35:13 -05:00

18 lines
505 B
ObjectPascal

##
function CheckISBN13(code: string): boolean;
begin
result := false;
code := code.Replace('-', '').Replace(' ', '');
if (code.Length <> 13) then exit;
var sum := 0;
foreach var digit in code index i do
if digit.isdigit then
sum += digit.ToDigit * (if i mod 2 = 0 then 1 else 3)
else exit;
result := sum mod 10 = 0;
end;
CheckISBN13('978-0596528126').Println;
CheckISBN13('978-0596528120').Println;
CheckISBN13('978-1788399081').Println;
CheckISBN13('978-1788399083').Println;