|
| 1 | +import java.util.*; |
| 2 | +import java.io.*; |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +//create ISBNNumberExample class to check whether the given number is a valid ISBN or not |
| 6 | +class ISBNNumberExample { |
| 7 | + |
| 8 | + static boolean checkISBNNumber(long number) |
| 9 | + { |
| 10 | + int sum = 0; |
| 11 | + int i, t, intNumber, dNumber; |
| 12 | + String strNumber; |
| 13 | + |
| 14 | + strNumber = ""+number; |
| 15 | + |
| 16 | + if (strNumber.length() != 10) { |
| 17 | + return false; |
| 18 | + } |
| 19 | + |
| 20 | + for (i = 0; i < strNumber.length(); i++) { |
| 21 | + intNumber = Integer.parseInt(strNumber.substring(i, i+1)); |
| 22 | + dNumber = i + 1; |
| 23 | + t = dNumber * intNumber; |
| 24 | + sum = sum + t; |
| 25 | + } |
| 26 | + |
| 27 | + // check whether the sum is divisible by 11 or not |
| 28 | + |
| 29 | + if ((sum % 11) == 0) { |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + return false; |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + // main() method start |
| 38 | + public static void main(String args[]) |
| 39 | + { |
| 40 | +long n1, n2; |
| 41 | + |
| 42 | + try { |
| 43 | + |
| 44 | + //create BufferedReader class object to get input from user |
| 45 | + InputStreamReader in = new InputStreamReader(System.in); |
| 46 | + BufferedReader br = new BufferedReader(in); |
| 47 | + |
| 48 | + //show custom message |
| 49 | + System.out.println("Enter first 10 digit ISBN number"); |
| 50 | + |
| 51 | + //store user entered value into variable n1 |
| 52 | + n1 = Long.parseLong(br.readLine()); |
| 53 | + |
| 54 | + //show custom message |
| 55 | + System.out.println("Enter second 10 digit ISBN number"); |
| 56 | + |
| 57 | + //store user entered value into variable n2 |
| 58 | + n2 = Long.parseLong(br.readLine()); |
| 59 | + |
| 60 | + if (checkISBNNumber(n1)) |
| 61 | + System.out.println(n1 + " is a valid ISBN number"); |
| 62 | + else |
| 63 | + System.out.println(n1 + " is not a valid ISBN number"); |
| 64 | + if (checkISBNNumber(n2)) |
| 65 | + System.out.println(n2 + " is a a valid ISBN number"); |
| 66 | + else |
| 67 | + System.out.println(n2 + " is not a valid ISBN number"); |
| 68 | + |
| 69 | + }catch(java.lang.Exception e) { |
| 70 | + System.out.println("Error while reading the data."); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments