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.