Problem description: The International Standard Book Number (ISBN) is a unique,
ID: 3549631 • Letter: P
Question
Problem description:
The International Standard Book Number (ISBN) is a unique, numerical commercial
book identifier. The ISBN is 13 digits long if assigned after January 1, 2007.
The last digit of the thireen-digit ISBN is called check digit, which is used to verify
if an ISBN is a valid ISBN. The following describes the way to calculate the check digit.
The calculation of an ISBN-13 check digit begins with the first 12 digits of the 13-digit ISBN
(thus excluding the check digit itself).
1) Each digit, from left to right, is alternately multiplied by 1 or 3,
2) those products are summed and calculated modulo 10 to give a value ranging from 0 to 9.
3) the value is subtracted from 10, that leaves a result from 1 to 10.
4) A zero (0) replaces a ten (10), so, in all cases, a single check digit results.
For example, the ISBN-13 check digit of 978-0-306-40615-? is calculated as follows:
s = 9
Explanation / Answer
//First Blank will be filled by
int checkDigit(string);
bool isValidISBN(string);
//second blank will be filled by
isValid = isValidISBN(isbn);
//Third blan filled by
int checkDigit(string ISBN)
{
int res=0;
for(int i=0;i<12;i++)
{
if(i%2==0)
{
res+=(ISBN[i]-'0')*1;
}
else
{
res+=(ISBN[i]-'0')*3;
}
}
res = res%10;
res = 10 - res;
return res;
}
//forth spance will be filled by
bool isValidISBN(string ISBN)
{
if((ISBN[12]-'0')==checkDigit(ISBN))
return true;
else
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.