Business: check ISBN-13) ISBN-13 is a new standard for identifying books. It uses 13 digits d1d2d3d4d5d6d7d8d9d10d11d12d13.

The last digit d13 is a checksum, which is calculated from the other digits using the following formula: 
 10-(d1 +3d2 +d3 +3d4 +d5 +3d6 +d7 +3d8 +d9 +3d10 +d11 +3d12)%10

If the checksum is 10, replace it with 0.
Your program should read the input as a string.
Here are sample runs: .

Enter the first 12 digits of an ISBN-13 as a string: 978013213080 The ISBN-13 number is 9780132130806
Enter the first 12 digits of an ISBN-13 as a string: 978013213079 The ISBN-13 number is 9780132130790
Enter the first 12 digits of an ISBN-13 as a string: 97801320 97801320 is an invalid input

Write the programm in C++

Respuesta :

Answer:

Following are the code to this question:

#include <iostream>//defining header file

#include <string.h>//defining header file

using namespace std;//using package

char checksum(const char *x)//defining a method checksum  

{

int s=0,m,d;//defining integer variables

for (m= 1;*x;++x)//defining for loop for caLculate value

{

d=*x-'0';//use integer variable to remove last digit from input value  

s=s+m*d;//add value in s variable

m=4-m;//use m to remove 4 from m

}

return '0'+(1000-s)%10;//use return keyword to get value  

}

int main()//defining main method

{

char ISBNNum[14];// defining character array  

cout<<"Enter the first 12 digits of an ISBN-13 as a string:";//print message

cin>>ISBNNum;//input value from user end

if(strlen(ISBNNum)==12)//defining if block to check length

{

char a=checksum(ISBNNum);//defining char variable a to call method checksum

ISBNNum[12]=a;//use array to hold method value

ISBNNum[13]='\0';// use array to hold 13 character value

cout<<"The ISBN-13 number is "<<ISBNNum;//print number

}

else//defining else block

{

cout<<ISBNNum<<" is an invalid input"<<endl;//print message

}

return 0;

}

output:

please find the attachment.

Explanation:

In the given code a character method "checksum" is define that accepts a character as a parameter and inside the method, three integer variable "s,m, and d" is defined that uses the for loop to calculate the input value and use the return keyword for the return value.  Inside the main method, a char array "ISBNNum", is defined that input the value from the user-end and define the conditional statement.

  • In the if block, it uses "ISBNNum" char array that checks its length equal to 12, if this condition is true it defines char a to call the method and hold its value and use the array to sore value in a and its 13 lengths it stores "\0", and print its value.
  • If the condition is false, it will print input array value and print invalid input as a message.
Ver imagen codiepienagoya