Code with Vinay

ISC practical papers › ISC 2013 › Question 1

Validate an ISBN

ISC 2013 Computer Science Practical, Question 1 · Number programs

The question

An ISBN ( International Standard Book Number) 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 ISBN is correct or not. Each of the first nine digits of the code can take a value between 0 and 9.

Sometimes it is necessary to make the last digit equal to ten; this is done by writing the last digit of the code as X.

To verify an ISBN, calculate 10 times the first digit, plus 9 times the second digit, plus 8 times the third and so on until we add 1 time the last digit.

If the final number leaves no remainder when divided by 11, the code is a valid ISBN.

For example:

02011003311 = 10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 = 55
Since 55 leaves no remainder when divisible by 11, hence it is a valid ISBN.

Design a program to accept a ten digit code from the user. For an invalid input, display an appropriate message. Verify the code for its validity in the format specified below:

Test your program with sample data and some random data.

Example 1
INPUT CODE : 0201530821
OUTPUT : SUM = 99
LEAVES NO REMAINDER – VALID ISBN CODE
Example 2
INPUT CODE : 035680324
OUTPUT : INVALID INPUT
Example 3
INPUT CODE : 0231428031
OUTPUT : SUM = 122
LEAVES REMAINDER – INVALID ISBN CODE

Question: ISC 2013 Computer Science Paper 2 (Practical), Question 1, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.

Java solution

ISC2013Q1.java Download GitHub
import java.util.*;

class ISBN {
    private String isbn;
    private int sum;

    public ISBN(String isbn) {
        this.isbn = isbn;
        if (isbn.length() != 10)
            throw new IllegalArgumentException();

        for (int position = 0; position < 9; position++) {
            if (!Character.isDigit(isbn.charAt(position))) {
                throw new IllegalArgumentException();
            }
        }
        char lastCharacter = isbn.charAt(9);
        if (!(Character.isDigit(lastCharacter) || (lastCharacter == 'x' || lastCharacter == 'X'))) {
            throw new IllegalArgumentException();
        }
        computeSum();
    }

    public void computeSum() {
        int digit;
        for (int position = 0; position < isbn.length(); position++) {
            // Note that the character from 0 to 9 has codes from 48 to 57
            digit = isbn.charAt(position) - '0'; // '0' will result in subtraction of 48
            digit = (digit == 40 || digit == 72) ? 10 : digit;
            sum += digit * (10 - position);
        }
    }

    public int getSum() {
        return sum;
    }
}

class ISC2013Q1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("INPUT CODE:\t");
        String isbn = in.nextLine();
        try {
            ISBN objISBN = new ISBN(isbn);
            System.out.println("OUTPUT:\tSUM = " + objISBN.getSum());
            if (objISBN.getSum() % 11 == 0) {
                System.out.println("\tLEAVES NO REMAINDER – VALID ISBN CODE");
            } else {
                System.out.println("\tLEAVES REMAINDER – INVALID ISBN CODE");
            }
        } catch (IllegalArgumentException e) {
            System.out.println("OUTPUT : INVALID INPUT");
        }
        in.close();
    }
}

All these solutions in one free book

Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.

Get the free PDF ₹0 ISC 2027 mock papers