ISBN Number ISC Computer Science 2013 Practical

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:

1. 0201103311 = 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 divided by 11, hence it is a valid ISBN.

2. 00746254X = 10 × 0 + 9 × 0 + 8 × 7 + 7 × 4 + 6 × 6 + 5 × 2 + 4 × 5 + 3 × 4 + 2 × 2 + 1 × 10 = 176
Since 176 leaves no remainder when divided by 11, hence it is a valid ISBN.

3. 0112112425 = 10 × 0 + 9 × 1 + 8 × 1 + 7 × 2 + 6 × 1 + 5 × 1 + 4 × 2 + 3 × 4 + 2 × 2 + 1 × 5 = 71
Since 71 leaves remainder when divided by 11, hence it is not 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

import java.util.Scanner;
class ISBN{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("INPUT CODE: ");
        String isbn = in.nextLine().toUpperCase();
        if(isbn.length() != 10){
            System.out.println("INVALID INPUT");
            return;
        }
        for(int i = 0; i < isbn.length() - 1; i++){
            char ch = isbn.charAt(i);
            if(!Character.isDigit(ch)){
                System.out.println("INVALID INPUT");
                return;
            }
        }
        char last = isbn.charAt(isbn.length() - 1);
        if(last != 'X' && !Character.isDigit(last)){
            System.out.println("INVALID INPUT");
            return;
        }
        int sum = 0;
        int num = 10;
        for(int i = 0; i < isbn.length(); i++){
            char ch = isbn.charAt(i);
            switch(ch){
            case '0':
                break;
            case '1':
                sum += num;
                break;
            case '2':
                sum += num * 2;
                break;
            case '3':
                sum += num * 3;
                break;
            case '4':
                sum += num * 4;
                break;
            case '5':
                sum += num * 5;
                break;
            case '6':
                sum += num * 6;
                break;
            case '7':
                sum += num * 7;
                break;
            case '8':
                sum += num * 8;
                break;
            case '9':
                sum += num * 9;
                break;
            case 'X':
                sum += num * 10;
                break;
            }
            num--;
        }
        System.out.println("SUM = " + sum);
        if(sum % 11 == 0)
            System.out.println("LEAVES NO REMAINDER - VALID ISBN CODE");
        else
            System.out.println("LEAVES REMAINDER - INVALID ISBN CODE");
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory