The 13-digit International Standard Book Number (ISBN) is a unique code that ide
ID: 2268058 • Letter: T
Question
The 13-digit International Standard Book Number (ISBN) is a unique code that iden- tifies a book commercially. The last digit is a check digit used for error detection. To calculate its value, each digit of the first twelve digits is alternately multiplied, from left to right, by 1 or 3. The products are summed up and divided by 10. The check digit is the remainder of the division subtracted from 10. If it is 10, it becomes 0. For example, assume that the first twelve digits are 978960931961. 5.8 (a) (G+1 + 7*3 8+1 93+ 6+1 + 0393*31*1+9*3 + 6*1 1 3)126 (b) The check digit = 10- (126 % 10) = 10-6 = 4 Write a program that reads a 13-digit ISBN and checks the last digit to see if it is valid or not.Explanation / Answer
Answer for "a":
#include <stdio.h>
#include <stdlib.h>
int main()
{
long long l,l1 ;
int i,j,sel;
int sum=0,checkDigit;
printf("Enter the number:");
scanf("%lld",&l);
for(i=1; i<13; i++)
{
l1=l;
for(j=1; j<=13-i; j++)
{
l1=l1/10;
}
l1=l1%10;
if(i%2==1)
{
sel=1;
}
else
{
sel=3;
}
sum+=sel*l1;
}
printf("sum==%d",sum);
checkDigit=10-(sum%10);
printf(" Check Digit=%d ",checkDigit);
if(checkDigit==(l%10))
{
printf("Valid ISBN");
}
else
{
printf("Invalid ISBN");
}
printf(" ");
return 0;
}
Output :
Enter the number : 9789609319614
Sum == 126
Check digit = 4
Valid ISBN
Process returned 0 <0*0> execution times: 13.243 s
Press any key to continue.
Answer for "b":
#include <stdio.h>
#include <stdlib.h>
int main()
{
long long l,l1 ;
int i,j,sel;
int sum=0,checkDigit;
printf("Enter the number:");
scanf("%lld",&l);
for(i=1; i<13; i++)
{
l1=l;
for(j=1; j<=13-i; j++)
{
l1=l1/10;
}
l1=l1%10;
if(i%2==1)
{
sel=1;
}
else
{
sel=3;
}
sum+=sel*l1;
}
printf("sum==%d",sum);
checkDigit=10-(sum%10);
printf(" Check Digit=%d ",checkDigit);
if(checkDigit==(l%10))
{
printf("Valid ISBN");
}
el
se
{
printf("Invalid ISBN");
}
printf(" ");
return 0;
}
output:
Enter the number: 1234567890123
Sum ==92
Check digit = 8
Invalid ISBN
Process returned 0 <0*0> execution times: 8.197 s
Press any key to continue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.