Respuesta :
Answer
input isbn
n = isbn.length
if n != 10 then
return false;
end if
// Computing weighted sum of first 9 digits
sum = 0
i = 0
do
digit = isbn[i] - '0'
if 0 > digit or 9 < digit then
return false
end if
sum = sum + (digit * (10 - i))
i=i + 1
while(i < 9)
// Checking last digit.
last = isbn[9]
if last != 'X' and (last < '0' or last > '9') then
return false;
end if
// If last digit is 'X', add 10 to sum, else add its value.
if last == 'X' then
sum = sum + 10
else
sum = sum + (last - '0')
end if
// Return true if weighted sum of digits is divisible by 11.
if sum % 11 == 0 then
Print sum
Explanation:.
The above algorithm reada an ISBN number and print out the checksum.
It is implemented base on the instructions in the question.
Line 1 accepts an ISBN number
Line 2 Calculates the length
Line 3 checks if the length is 10.
If no the algorithm is stopped.
Line 8 initialise the checksum to 0
Line 12 through 17 Calculates the step 1 of the checksum using an iterative do statement
Line 19 reads the last character of the ISBN number
If the last character is not X and it is not a number, the algorithm is stopped
Line 24 to 28 adds or remove the last character from the checksum
The last 2 lines check if the checksum can be divided by 11.
If yes, the checksum is printed