-
Notifications
You must be signed in to change notification settings - Fork 14
/
ad2.java
74 lines (56 loc) · 2.31 KB
/
ad2.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//import required classes and packages
import java.util.*;
import java.io.*;
import java.util.Scanner;
//create ISBNNumberExample class to check whether the given number is a valid ISBN or not
class ISBNNumberExample {
static boolean checkISBNNumber(long number)
{
int sum = 0;
int i, t, intNumber, dNumber;
String strNumber;
strNumber = ""+number;
if (strNumber.length() != 10) {
return false;
}
for (i = 0; i < strNumber.length(); i++) {
intNumber = Integer.parseInt(strNumber.substring(i, i+1));
dNumber = i + 1;
t = dNumber * intNumber;
sum = sum + t;
}
// check whether the sum is divisible by 11 or not
if ((sum % 11) == 0) {
return true;
}
return false;
}
// main() method start
public static void main(String args[])
{
long n1, n2;
try {
//create BufferedReader class object to get input from user
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
//show custom message
System.out.println("Enter first 10 digit ISBN number");
//store user entered value into variable n1
n1 = Long.parseLong(br.readLine());
//show custom message
System.out.println("Enter second 10 digit ISBN number");
//store user entered value into variable n2
n2 = Long.parseLong(br.readLine());
if (checkISBNNumber(n1))
System.out.println(n1 + " is a valid ISBN number");
else
System.out.println(n1 + " is not a valid ISBN number");
if (checkISBNNumber(n2))
System.out.println(n2 + " is a a valid ISBN number");
else
System.out.println(n2 + " is not a valid ISBN number");
}catch(java.lang.Exception e) {
System.out.println("Error while reading the data.");
}
}
}