
An International Standard Book Number (ISBN) is a unique number
assigned to each book. ISBN-13 has thirteen digits in 5 parts which
the last digit is “check digit”. The check digit is base ten, and can
be 0-9. To compute a missing check digit, each digit, from the left
to right, is alternatively multiplied by 1 or 3.Then, the sum of these
|
products should be divided by 10 to find the remainder ranging
from 0 to 9. Finally, the check digit can be found by subtracting the
remainder from 10, that leaves a result from 1 to 10.
|
For example, take the ISBN 978-0-306-40615-? :
-
a) Firstcalculatesumofproducts:9×1+7×3+8×1+0×3+3×1+0×3+6×1+4×3
+ 0×1 + 6×3 + 1×1 + 5×3 = 93
-
b) Remainder upon dividing by 10 is 3.
-
c) Remainder is subtracted from 10.
-
d) Check digit is 7 (10 - 3).
{
// variable declaration
unsigned long long int isbn, copyISBN;
int count = 1, sum = 0, remainder, digit, checkDigit;
// accept user input
printf("Enter the first twelve digits of an ISBN-13: ");
scanf("%llu", &isbn);
// copy this isbn to copyISBN
copyISBN = isbn;
// loop until copyISBN > 0
while (copyISBN > 0)
{
// get last digit of copyISBN
digit = copyISBN % 10;
// if even then multiply by one
if (count % 2==0)
{
sum = sum + (digit * 1);
count = 1;
}
// else multiply by 3
else
{
sum = sum + (digit * 3);
count = 2;
}
// divide copyISBN by 10
copyISBN = copyISBN / 10;
}
// find remainder
remainder = sum % 10;
// find checkDigit
checkDigit = 10 - remainder;
// print output
printf("Check Digit: %d", checkDigit);
return 0;
}
Comments
Post a Comment