SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

ISBN Number Validation Program in Java and Python

11 February 2013

ISBN number validation program with algorithm, explanation, Java solution and simple Python solution for ISC students.

Question:

An ISBN is a ten digit code which uniquely identifies a book. The first nine digits represent the group, publisher and title of the book, and the last digit is used to check whether the ISBN is correct.

Each of the first nine digits can take a value from 0 to 9. Sometimes the last digit has value 10; this is represented by writing X.

To verify an ISBN, calculate 10 times the first digit, 9 times the second digit, 8 times the third digit and so on until 1 times the last digit. If the final sum is divisible by 11, the code is a valid ISBN.

INPUT CODE: 0201530821 OUTPUT: SUM = 99 LEAVES NO REMAINDER - VALID ISBN CODE INPUT CODE: 035680324 OUTPUT: INVALID INPUT INPUT CODE: 0231428031 OUTPUT: SUM = 122 LEAVES REMAINDER - INVALID ISBN CODE

Algorithm:

Step 1: Start.

Step 2: Accept the ISBN code as a string.

Step 3: If the length is not 10, display INVALID INPUT.

Step 4: Initialize sum as 0 and multiplier as 10.

Step 5: Read each character of the ISBN from left to right.

Step 6: For the first nine positions, accept only digits from 0 to 9.

Step 7: For the last position, accept either a digit or X.

Step 8: Multiply each digit value by the current multiplier and add it to sum.

Step 9: Decrease the multiplier after every character.

Step 10: If sum is divisible by 11, display valid ISBN; otherwise display invalid ISBN.

Step 11: Stop.

Explanation:

The ISBN is processed as a string because the last character may be X instead of a digit. This also keeps leading zeroes safe, which is important in ISBN codes.

The multiplier starts from 10 for the first character and decreases by 1 after each position. This directly follows the ISBN checking rule given in the question.

For positions 1 to 9, only numeric digits are valid. At the last position, X is allowed and is treated as the value 10.

After the weighted sum is calculated, divisibility by 11 decides validity. A remainder of 0 means the ISBN code is valid; any other remainder means it is invalid.

The implementation therefore uses both position and value: the loop index decides the multiplier, while the character value decides what number is added. This is why the input must be processed one character at a time instead of converting the whole ISBN to an integer.

ISBN validation uses a weighted sum of digits. Each digit is multiplied by a weight based on its position, and all products are added. For a valid 10-digit ISBN, the final sum must be divisible by 11. The last character may be X, which represents the value 10, so the program must handle that case separately. The solution is mainly about position-based multiplication and careful validation of input length and allowed characters before applying the divisibility test.

Java Program:

Java
/**
* The class ISBN_ISC2013 inputs a 10 digit code and checks whether it is a valid ISBN code or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2013 Question 1
*/

import java.util.Scanner;
class ISBN_ISC2013
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a 10 digit code : ");
        String s=sc.nextLine();

        int len=s.length();

        if(len!=10)
        System.out.println("Output : Invalid Input");
        else
        {
            char ch;
            int dig=0, sum=0, k=10;
            for(int i=0; i<len; i++)
            {
                ch=s.charAt(i);
                if(ch=='X')
                dig=10;
                else
                dig=ch-48;
                sum=sum+dig*k;
                k--;
            }

            /*Alternate Code which can be used instead of the above code

            String ch;
            int dig=0, sum=0, k=10;
            for(int i=0; i<len; i++)
            {
            ch=Character.toString(s.charAt(i));
            if(ch.equalsIgnoreCase("X"))
            dig=10;
            else
            dig=Integer.parseInt(ch);
            sum=sum+dig*k;
            k--;

            */

            System.out.println("Output : Sum = "+sum);
            if(sum%11==0)
            System.out.println("Leaves No Remainder - Valid ISBN Code");
            else
            System.out.println("Leaves Remainder - Invalid ISBN Code");
        }
    }
}

Equivalent Python Program:

Python
# Read the code as a string so each digit/character position can be checked.
# Position-based loop logic applies the required checksum calculation.
# Use the final remainder/check value to decide and display validity.

code = input("INPUT CODE: ")

if len(code) != 10:
    print("INVALID INPUT")
else:
    total = 0
    multiplier = 10
    valid = True

    for i in range(10):
        ch = code[i]
        if ch >= '0' and ch <= '9':
            value = ord(ch) - ord('0')
        elif i == 9 and (ch == 'X' or ch == 'x'):
            value = 10
        else:
            valid = False
            break

        total = total + value * multiplier
        multiplier = multiplier - 1

    if valid == False:
        print("INVALID INPUT")
    else:
        print("SUM =", total)
        if total % 11 == 0:
            print("LEAVES NO REMAINDER - VALID ISBN CODE")
        else:
            print("LEAVES REMAINDER - INVALID ISBN CODE")

Output:

INPUT CODE: 0201530821 OUTPUT: SUM = 99 LEAVES NO REMAINDER - VALID ISBN CODE INPUT CODE: 035680324 OUTPUT: INVALID INPUT INPUT CODE: 0231428031 OUTPUT: SUM = 122 LEAVES REMAINDER - INVALID ISBN CODE

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →