ISBN Number in JAVA [Updated]

Program to Check ISBN Number

ISBN Number: ISBN stands for International Standard Book Number.

It is a unique numeric book identifier which is printed on every book.

The ISBN is based upon 10 Digit Code.

The ISBN Number is valid if :-

(10 X Digit 1 + 9 X Digit 2 + 8 X Digit 3 + 7 X Digit 4 + 6 Digit 5 + 5 X Digit 6 + 4 X Digit 7 + 3 X Digit 8 + 2 Digit 9 Digit 10)
is Divisible by 11.


Example:

ISBN : 1401601499
Sum : 10*1 + 9*4 + 8*0 + 7*1 + 6*6 + 5*0 + 4*1 + 3*4 + 2*9 + 1*9 = 132
132 is divisible by 11

Output : Correct ISBN number.

Algorithm and Program given below :-

Algorithm :-

  • Input ISBN number in a String variable
  • store the length of the string in a variable
  • Check if length is equal to 10 or not
  • If it is not equal to 10 then print "Invalid ISBN Number"
  • Otherwise check If 'X' is present at any position (in the first 9 positions of the ISBN number). If 'X' is present print "Illegal ISBN Number"
  • Otherwise store the sum of first 9 digits by multiplying the digit with respective position number.
  • Now check if the 10th digit (i.e. the last digit) is 'X' or an integer
  • if it is 'X' or 'x'. Add 10 in the previous sum
  • Otherwise add the respective integer to the previous sum.
  • Now, check if the Final sum is divisible by 11 or not.
  • If it is divisible print "Legal ISBN number"
  • Otherwise print "Illegal ISBN Number"

Program :-

import java.io.*;
import java.util.*;
class ISBN1
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int l,c=0,i,sum=0;
        String str;
        char ch;
        System.out.println("Enter ISBN Number");
        str = in.nextLine();
        l= str.length();
        if(l!=10)
        {
            System.out.println("Illegal ISBN Number");
        }
        else
        {
            for(i=0;i<l-1;i++)
            {
                c= 10-i; //storing position number
                ch= str.charAt(i); // extracting digit
                if(ch=='X'||ch=='x')
                {
                    System.out.println("Illegal ISBN Number");
                    break;
                }
                else
                {
                    sum= sum+(Integer.parseInt(String.valueOf(ch)))*c; //storing sum upto 9th digit
                }
            }
            if(str.charAt(l-1)=='X'||str.charAt(l-1)=='x')
            {
                sum= sum+10; //Storing the sum of digits if the check digit is X
            }
            else
            {
                sum=sum+(Integer.parseInt(String.valueOf(str.charAt(l-1)))); //Storing the sum of digits if the check digit is an Integer
            }
            if(sum%11==0)
            {
                System.out.println("Valid ISBN Number");
            }
            else
            {
                System.out.println("Invalid ISBN Number");
            }
        }
    }//end of main
}// end of class

Comments

Popular posts from this blog

Frequency of each digit of a number in Java

Trimorphic Number in JAVA

Tri-Automorphic Number in JAVA