In this tutorial you will learn about the ISBN Number Program In Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to find ISBN Number Program In Java
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- Function in java.
International Standard Book Number (ISBN) IN JAVA Program
In world every book has a number called ‘International Standard Book Number (ISBN).It is unique number. With the help of this number, we can easily find any book in the world. ISBN is a 10 digit number. The number is legal if sum of (1*digit1 + 2*digit2 + 3*digit3 + 4*digit4 + 5*digit5 + 6*digit6 + 7*digit7 + 8*digit8 + 9*digit9 + 10*digit10) is divisible by sum%11==0 gives remainder Zero.
Let’s take a Number = 0306406152 the find sum
Sum = (0*1) + (3*2) + (0*3) + (6*4) + (4*5) + (0*6) + (6*7) + (1*8) + (5*9) + (2*10)
–“– = 0 + 6 + 0 + 24 + 20 + 0 + 42 + 8 + 45 + 20
Sum = 165, and 165 / 11 = 15. So 165 is divisible by 11 then the number is International Standard Book Number (ISBN).
Java Program to check the number is ISBN Number Program or not.
In this program we would find the given number is tech number first will take the input from the user and check whether it is Tech number or not. If it’s not then the program will show the output as it’s not a tech 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 |
// java program to find ISBN number import java.io.*; class ISBNumber { public static void main(String[] args) throws IOException { // Delacig variables.. long isbn_no; int c = 0, i, t, d, dNo; String str; // Input a 10-digit ISBN number from user InputStreamReader in = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(in); System.out.print("enter a 10-digit ISBN number: "); isbn_no = Long.parseLong(br.readLine()); // checking the length is 10 or not of given number else , exit from the program str = "" + isbn_no; if (str.length() != 10) { System.out.println("Illegal ISBN"); return; } // : Logic for an ISBN 0201530821 //S = 0*10 + 2*9 + 0*8 + 1*7 + 5*6 + 3*5 + 0*4 + 8*3 + 2*2 + 1*1 = 99 // if divisible by 11. // compute the digits c = 0; for (i = 0; i < str.length(); i++) { d = Integer.parseInt(str.substring(i, i + 1)); dNo = i + 1; t = dNo * d; c += t; } // checking condtion is divisible by 11 or not if ((c % 11) != 0) { System.out.println("Illegal ISBN"); } else { System.out.println("Legal ISBN"); } } } |
Output
In the above program, we have first declared and initialized a set variables required in the program.
- isbn_no = it will hold entered number.
- c = 0 itr will count length.
- i= for iteration .
- c= calculate the sum of number.
After that we take 10 digit number form user.
Then we check that given number is a 10 digit number or not.
if not we we exit from program
else we proceed the logic for ISbn number as shown in image below.
Find isbn by multiplying each digit from left to right by 1, 2, 3,…….,10.
and at last we will check if number is completely divisible by 11 then it is a leagal isbn number if not it is not a isbn number.