Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. Read the string isbn. Set sum = 0 and status = 0.
  2. If its length is not 10, set status = 1 (wrong length).
  3. Otherwise go through the ten characters with i from 0 to 9, stopping at the first fault:
    • a digit character has its numeric value ch - '0';
    • a capital X is accepted only as the last character (i == 9) and has the value 10;
    • any other character sets status = 2 (invalid character).
  4. Add v * (10 - i) to sum: the weight is 10 for the first character, down to 1 for the last.
  5. If every character was acceptable (status == 0) and sum % 11 != 0, set status = 3 (checksum fails).
  6. If status is 0 print VALID ISBN and the weighted sum. Otherwise print INVALID ISBN and 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
isbnStringThe string typed by the user
sumintWeighted sum of the ten values
statusint0 = valid, 1 = wrong length, 2 = invalid character, 3 = checksum fails
iintPosition of the character being examined, 0 to 9
chcharThe character at position i
vintNumeric value of ch

Sample input and output

Sample 1

Enter the ISBN: 0306406152
VALID ISBN
Weighted sum = 132

Sample 2

Enter the ISBN: 080442957X
VALID ISBN
Weighted sum = 209

Sample 3

Enter the ISBN: 0306406153
INVALID ISBN - checksum fails

Sample 4

Enter the ISBN: 12345
INVALID ISBN - wrong length

Sample 5

Enter the ISBN: 03064X6152
INVALID ISBN - invalid character

Sample 6

Enter the ISBN: 03064a6152
INVALID ISBN - invalid character

One value per line

Dry run

Take the input $0306406152$. The length is 10, so the loop runs for i from 0 to 9. The weight is 10 - i.

ichvWeightv x weightsum
0001000
13392727
2008027
36674269
44462493
5005093
666424117
71133120
855210130
92212132

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

  1. 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."
  2. Check digit "Accept the first 9 digits of an ISBN-10 and find its check character."