contestada

2.ISBN. The International Standard Book Number (ISBN) is a 10-digit code that uniquely specifies a book. The rightmost digit is a checksum digit that can be uniquely determined from the other 9 digits in the following manner: • Multiply the first digit by 10, the second by 9, the third by 8, and so on, down to the ninth digit by 2. . Add the values together. • Divide the sum by 11. What is the remainder? • If the remainder is 0, the checksum is 0; otherwise, subtract the remainder from 11 to get the checksum digit. If the checksum that you come up with is 10, then X is used as the checksum digit! As an example, the checksum digit corresponding to 020131452 is 5 since 5 is the only value of x between O and 10 for which 10*0+992 +840 + 7*1 + 6*3 + 5*1 + 4*4 + 35 + 2*2 + 1*x is a multiple of 11. Write an algorithm that reads an integer number of 9 digits and displays the checksum digit of the ISBN Hint 1: to extract the rightmost digit of a number use: COMPUTE digit AS number MODULUS 10 Hint 2: to remove the rightmost digit of a number use: COMPUTE number AS number / 10 Hint 3: for full credit use a loop to compute the sum Assume the input value is a 9-digit positive integer. Also assume that the input is a valid ISBN number without the checksum digit.

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