java program for isbn number

ISBN IN JAVA Program


ISBN IN JAVA Program


Almost every book carry number called ‘The International Standard Book Number (ISBN)’ which is a unique number. By this number, we can find any book. It is a 10 digit number. The ISBN is legal if 1*digit1 + 2*digit2 + 3*digit3 + 4*digit4 + 5*digit5 + 6*digit6 + 7*digit7 + 8*digit8 + 9*digit9 + 10*digit10 is divisible by 11.
Example: For an ISBN "1259060977"
Sum = 1*10 + 2*9 + 5*8 + 9*7 + 0*6 + 6*5 + 0*4 + 9*3 + 7*2 + 7*1 = 209
Now divide it with 11 = 20%/11 = 0. If the resultant value will be Zero then it is a valide ISBN.
Write a program to:
(i) input the ISBN code as a 10-digit number
(ii) If the ISBN is not a 10-digit number, output the message “Illegal ISBN” and terminate the program
(iii) If the number is 10-digit, extract the digits of the number and compute the sum as explained above.

If the sum is divisible by 11, output the message “Legal ISBN”. If the sum is not divisible by 11, output the message “Illegal ISBN”.

Solution

import java.io.*;

/**
* Khurshid Md Anwar #inspireSkill
*/
class ISBNumber {

    public static void main(String[] args) throws IOException {
        long isbnNumber;
        int s = 0, i, t, d, dNumber;
        String st;
        // Input a 10-digit ISBN number
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Input a 10-digit ISBN number: ");
        isbnNumber = Long.parseLong(br.readLine());

        // check the length is 10, otherwise, exit from the program
        st = "" + isbnNumber;
        if (st.length() != 10) {
            System.out.println("Illegal ISBN");
            return;
        }
        // : For an ISBN 1259060977
        //S = 1*10 + 2*9 + 5*8 + 9*7 + 0*6 + 6*5 + 0*4 + 9*3 + 7*2 + 7*1 = 209
        // which is divisible by 11.
        // compute the s of the digits
        s = 0;
        for (i = 0; i < st.length(); i++) {
            d = Integer.parseInt(st.substring(i, i + 1));
            dNumber = i + 1;
            t = dNumber * d;
            s += t;
        }

        // check the number s is divisible by 11 or not
      
        if ((s % 11) != 0) {
            System.out.println("Illegal ISBN");
        } else {
            System.out.println("Legal ISBN");
        }
    }
}

Output

Input a 10-digit ISBN number: 1259060977
Legal ISBN
Input a 10-digit ISBN number: 1245876589
Legal ISBN
Input a 10-digit ISBN number: 1234567890
Illegal ISBN

Click for more number program


Core Java: An Integrated Approach, New: Includes All Versions upto Java 8


SHARE THIS
Previous Post
Next Post