Java Program to Check ISBN Number

In the previous article, we have discussed Java Program to Check Evil Number

In this article we are going to understand what ISBN number is and how we can check whether a number is ISBN number or not in Java with examples.

Program to Check ISBN Number

ISBN or Intenrational Standard Book Number is used to identify a book anywhere in the world. These numbers contain ten digits. When each digit is multiplied with its position, if the result is divisible by 11 it is said to be a genuine ISBN number.

 Example :

  • 1259060977:

(1*1)+(2*2)+(5*3)+(9*4)+(0*5)+(6*6)+(0*7)+(9*8)+(7*9)+(7*10)= 209

209%11=0 ISBN number

  • 8147852369:

(8*1)+(1*2)+(4*3)+(7*4)+(8*5)+(5*6)+(2*7)+(3*8)+(6*9)+(9*10)= 281

281%11=6 Not an ISBN number

In the above examples the numbers 1259060977 is an ISBN numbers as the resultant sum is divisible by 11. However 8147852369 is not a ISBN number as it leaves a remainder of 6 when the sum is divided by 11.

Let’s see different ways to check ISBN number.

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Approach :

  1. Enter/declare a number and store it.
  2. We multiply the digits with their respective positions.
  3. We add the sum of all digits.
  4. If the resultant sum is exactly divisible by 11, then the number is said to be an ISBN number.

Method-1: Java Program to Check ISBN Number By Using Static Value

import java.util.Scanner;

public class ISBNNumber
{
    public static void main(String args[])
    {
       
        Scanner scan = new Scanner(System.in);
        
        //A number declared
        int num = 1259060977;

        int count = 0,temp = num, sum = 0, remainder;
        
        // Loop to iterate the digits and to find the sum
        while(temp>0)
        {
            remainder = temp%10;
            temp /= 10;
            sum+= remainder*++count;
        }

        //dividing with 11 and checking condition
        if(sum%11==0)
        {
            System.out.println(num+" is an ISBN number");
        }
        else
        {
            System.out.println(num+" is Not an ISBN number");
        }
    }
}
Output:

1259060977 is an ISBN number

Method-2: Java Program to Check ISBN Number By User Input Value

import java.util.Scanner;

public class ISBNNumber
{
    public static void main(String args[])
    {
       
        Scanner scan = new Scanner(System.in);
        
        //Taking the number as input 
        //from the user using scanner class
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        int count = 0,temp = num, sum = 0, remainder;
        
        // Loop to iterate the digits and to find the sum
        while(temp>0)
        {
            remainder = temp%10;
            temp /= 10;
            sum+= remainder*++count;
        }

        //dividing with 11 and checking condition
        if(sum%11==0)
        {
            System.out.println(num+" is an ISBN number");
        }
        else
        {
            System.out.println(num+" is Not an ISBN number");
        }
    }
}
Output:

Case-1

Enter a number : 1259060977 
1259060977 is an ISBN number

Case-2

Enter a number : 1111111111 
1111111111 is an ISBN number


Case-3

Enter a number : 1239059023 
1239059023 is Not an ISBN number

Method-3: Java Program to Check ISBN Number By Using User Defined Method

import java.util.Scanner;

public class ISBNNumber
{
    public static void main(String args[])
    {
       
        Scanner scan = new Scanner(System.in);
        
        //Taking the number as input 
        //from the user using scanner class
        System.out.print("Enter a number : ");
        int num = scan.nextInt();
        //calling the user defined method
        //to check ISBN number or not.
        checkNumber(num);
    }

    //checkNumber() method to check ISBN number
    public static void checkNumber(int num)
    {

        int count = 0,temp = num, sum = 0, remainder;
        
        // Loop to iterate the digits and to find the sum
        while(temp>0)
        {
            remainder = temp%10;
            temp /= 10;
            sum+= remainder*++count;
        }

        //dividing with 11 and checking condition
        if(sum%11==0)
        {
            System.out.println(num+" is an ISBN number");
        }
        else
        {
            System.out.println(num+" is Not an ISBN number");
        }
    }
}
Output: 

Case-1 

Enter a number : 1259060977 
1259060977 is an ISBN number 

Case-2 

Enter a number : 1111111111 
1111111111 is an ISBN number 

Case-3 

Enter a number : 1239059023 
1239059023 is Not an ISBN number

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: