ISC Practicals and Theory - ISBN-10 Validator
Updated 21 Oct 2026
An ISBN-10 is the old 10-character code that identifies a book. Its last character is a check character, so a mistyped code can be detected by a simple calculation. This is the Main version: everything is written in the main method, which reads the string, tests it and reports the result.
The program prints VALID ISBN with the weighted sum, or INVALID ISBN with the reason: wrong length, invalid character or checksum fails. Only a capital X is accepted as the check character, and only in the last position; a small x is rejected as an invalid character.
Definition
A string is a valid ISBN-10 when:
- it has exactly 10 characters;
- the first 9 are digits and the 10th is a digit or the letter X, which stands for the value 10;
- the weighted sum $10 d_1 + 9 d_2 + 8 d_3 + \ldots + 2 d_9 + 1 d_{10}$ is divisible by 11.
For example, $0306406152$ gives $0 \times 10 + 3 \times 9 + 0 \times 8 + 6 \times 7 + 4 \times 6 + 0 \times 5 + 6 \times 4 + 1 \times 3 + 5 \times 2 + 2 \times 1 = 132$, and $132 = 11 \times 12$, so it is valid. Changing the last digit to 3 gives 133, which is not divisible by 11, so $0306406153$ is invalid.
Algorithm
- Read the string
isbn. Setsum = 0andstatus = 0. - If its length is not 10, set
status = 1(wrong length). - Otherwise go through the ten characters with
ifrom 0 to 9, stopping at the first fault:- a digit character has its numeric value
ch - '0'; - a capital
Xis accepted only as the last character (i == 9) and has the value 10; - any other character sets
status = 2(invalid character).
- a digit character has its numeric value
- Add
v * (10 - i)tosum: the weight is 10 for the first character, down to 1 for the last. - If every character was acceptable (
status == 0) andsum % 11 != 0, setstatus = 3(checksum fails). - If
statusis 0 printVALID ISBNand the weighted sum. Otherwise printINVALID ISBNand the reason for 1, 2 or 3.
Program
Everything is written inside main: one for loop gives each character its value and weight, and the status code decides the message.
import java.util.Scanner;
/* Program ISBN10 checks an ISBN-10.
A valid ISBN-10 has exactly 10 characters: 9 digits, then a check character that is
a digit or the capital letter X (value 10). The weighted sum
d1 x 10 + d2 x 9 + ... + d10 x 1 must be divisible by 11. */
public class ISBN10
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the ISBN: ");
String isbn = sc.nextLine(); // the string typed by the user
int sum = 0; // weighted sum of the ten values
int status = 0; // 0 = valid, 1 = wrong length, 2 = invalid character, 3 = checksum fails
if (isbn.length() != 10)
{
status = 1;
}
else
{
for (int i = 0; i < 10 && status == 0; i++)
{
char ch = isbn.charAt(i); // character at position i
int v = 0; // numeric value of ch
if (ch >= '0' && ch <= '9')
v = (int) (ch - '0'); // digit character to its value
else if (ch == 'X' && i == 9)
v = 10; // capital X is allowed only as the last character
else
status = 2; // any other character is invalid
sum = sum + v * (10 - i); // weight is 10 for the first character down to 1
}
if (status == 0 && sum % 11 != 0)
status = 3;
}
if (status == 0)
{
System.out.println("VALID ISBN");
System.out.println("Weighted sum = " + sum);
}
else if (status == 1)
System.out.println("INVALID ISBN - wrong length");
else if (status == 2)
System.out.println("INVALID ISBN - invalid character");
else
System.out.println("INVALID ISBN - checksum fails");
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
isbn | String | The string typed by the user |
sum | int | Weighted sum of the ten values |
status | int | 0 = valid, 1 = wrong length, 2 = invalid character, 3 = checksum fails |
i | int | Position of the character being examined, 0 to 9 |
ch | char | The character at position i |
v | int | Numeric value of ch |
Sample input and output
Sample 1
Enter the ISBN: 0306406152
VALID ISBN
Weighted sum = 132Sample 2
Enter the ISBN: 080442957X
VALID ISBN
Weighted sum = 209Sample 3
Enter the ISBN: 0306406153
INVALID ISBN - checksum failsSample 4
Enter the ISBN: 12345
INVALID ISBN - wrong lengthSample 5
Enter the ISBN: 03064X6152
INVALID ISBN - invalid characterSample 6
Enter the ISBN: 03064a6152
INVALID ISBN - invalid characterDry run
Take the input $0306406152$. The length is 10, so the loop runs for i from 0 to 9. The weight is 10 - i.
| i | ch | v | Weight | v x weight | sum |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 10 | 0 | 0 |
| 1 | 3 | 3 | 9 | 27 | 27 |
| 2 | 0 | 0 | 8 | 0 | 27 |
| 3 | 6 | 6 | 7 | 42 | 69 |
| 4 | 4 | 4 | 6 | 24 | 93 |
| 5 | 0 | 0 | 5 | 0 | 93 |
| 6 | 6 | 6 | 4 | 24 | 117 |
| 7 | 1 | 1 | 3 | 3 | 120 |
| 8 | 5 | 5 | 2 | 10 | 130 |
| 9 | 2 | 2 | 1 | 2 | 132 |
Since $132 \% 11 = 0$, status stays 0 and the program prints VALID ISBN and Weighted sum = 132, the same as the first sample run.
Variations
- ISBN-13 "Write a program to check whether a 13-digit ISBN is valid, using the weights 1 and 3 alternately and a sum divisible by 10."
- Check digit "Accept the first 9 digits of an ISBN-10 and find its check character."