In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java
ISBN number program in java.
ISBN is a 10-digit unique number that is is carried by almost each and every book. It is used to identify the book uniquely. With the help of ISBN, we can easily find a book. 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.
The first nine digits of the ISBN number are used to represent the Title, Publisher, and Group of the book, and the last digit is used for checking whether ISBN is correct or not.
Example:
Let us consider a number: 1259060977
Check:
Sum = 1*10 + 2*9 + 5*8 + 9*7 + 0*6 + 6*5 + 0*4 + 9*3 + 7*2 + 7*1 = 209
Now divide the sum by 11 and see if the remainder obtained is zero or not.
rem = 209%11 = 0
Since, rem = 0, therefore 1259060977 is a legal ISBN number.
Steps to write Program for ISBN number Java
- First, take a 10-digit number input from the user.
- The second step is to check if the entered number is 10-digits or not because a number cannot be ISBN if it is not a 10-digit number. If it is not a 10-digit number then we will display an invalid message otherwise follow the steps below.
- For the sum, we need to multiply each digit by 1 to 10 from left to right.
- Lastly, divide the sum and check if the remainder is zero or not, if it is zero then the entered number is an ISBN number else it is not.
Now let us implement the above logic to check whether the number is an ISBN or not in java.
Java Program to check for ISBN Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | //Program to check the ISBN number in java import java.io.*; public class ISBNumberProgram { public static void main(String[] args) throws IOException { long num; int sum = 0, i, t, d, dNumber; String numString; // User input InputStreamReader in = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(in); System.out.print("Enter 10-digit ISBN number: "); num = Long.parseLong(br.readLine()); //check if the entered digits are total 10 digits or not numString = "" + num; if (numString.length() != 10) { System.out.println("Invalid Number"); return; //the program will terminate } // computing sum by iterating each digits for (i = 0; i < numString.length(); i++) { d = Integer.parseInt(numString.substring(i, i + 1)); dNumber = i + 1; t = dNumber * d; sum += t; } //lastly check and display the result if ((sum % 11) != 0) { System.out.println(num+ " is an Illegal ISBN Number"); } else { System.out.println(num+ " is a Legal ISBN Number"); } } } |
Output: After execution following result will be displayed.
//Run: 1
Enter 10-digit ISBN number: 1259060977
1259060977 is a Legal ISBN Number
//Run: 2
Enter 10-digit ISBN number: 8417652579
8417562579 is an Illegal ISBN Number
You may create a separate user define function where you can pass the entered number and do the necessary calculation and at the end return the result to the main function. This is how you calculate the ISBN number java.