#C4137. ISBN-13 Validation
ISBN-13 Validation
ISBN-13 Validation
In this problem, you are required to determine whether a provided ISBN-13 code is valid. The valid ISBN-13 code is defined according to the following rules:
- The code must be exactly 13 characters long.
- Every character must be a decimal digit (0-9).
- The final (13th) digit is a check digit computed from the first 12 digits using the formula below.
The check digit is calculated as follows:
Let \(d_1, d_2, \ldots, d_{12}\) be the first 12 digits. Compute the weighted sum: \[ S = d_1 + 3d_2 + d_3 + 3d_4 + \cdots + d_{11} + 3d_{12} \] Then the check digit \(d_{13}\) is given by: \[ d_{13} = (10 - (S \bmod 10)) \bmod 10. \]
Your program should read an ISBN-13 code from standard input and print True
if the code is valid, and False
otherwise.
For example, the ISBN "9780470059029" is valid, whereas "9780470059025" is not.
inputFormat
The input consists of a single line containing a string representing the ISBN-13 code.
Note: The input is given via standard input.
outputFormat
If the given ISBN-13 code is valid according to the rules described above, output True
; otherwise, output False
(both without quotes). The output should be printed using standard output.
9780470059029
True