Answer:
<em>The programming language is not stated;</em>
<em>However, I'll answer this question using C++ programming language</em>
<em>The program uses few comments; See explanation section for more detail</em>
<em>Also, the program assumes that all input will always be an integer</em>
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
string input;
cout<<"Enter the first 9 digits of an ISBN as a string: ";
cin>>input;
//Check length
if(input.length() != 9)
{
cout<<"Invalid input\nLength must be exact 9";
}
else
{
int num = 0;
//Calculate sum of products
for(int i=0;i<9;i++)
{
num += (input[i]-'0') * (i+1);
}
//Determine checksum
if(num%11==10)
{
input += "X";
cout<<"The ISBN-10 number is "<<input;
}
else
{
ostringstream ss;
ss<<num%11;
string dig = ss.str();
cout<<"The ISBN-10 number is "<<input+dig;
}
}
return 0;
}
Explanation:
string input; -> This line declares user input as string
cout<<"Enter the first 9 digits of an ISBN as a string: "; -> This line prompts the user for input
cin>>input; -> The user input is stored here
if(input.length() != 9) {
cout<<"Invalid input\nLength must be exact 9"; } -> Here, the length of input string is checked; If it's not equal to then, a message will be displayed to the screen
If otherwise, the following code segment is executed
else {
int num = 0;
-> The sum of products of individual digit is initialized to 0
The sum of products of individual digit is calculated as follows
for(int i=0;i<9;i++)
{
num += (input[i]-'0') * (i+1);
}
The next lines of code checks if the remainder of the above calculations divided by 11 is 10;
If Yes, X is added as a suffix to the user input
Otherwise, the remainder number is added as a suffix to the user input
if(num%11==10) { input += "X"; cout<<"The ISBN-10 number is "<<input;
}
else
{
ostringstream ss;
ss<<num%11;
string dig = ss.str();
cout<<"The ISBN-10 number is "<<input+dig;
}
}