-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMEICheck.java
55 lines (50 loc) · 1.46 KB
/
IMEICheck.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
package algorithms;
import java.util.Scanner;
/**
*
* @author Gökhan DAĞTEKİN
*
* 2015
*/
public class IMEICheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter IMEI number(15 Character): ");
String IMEINumber = sc.next();
int[] IMEINumberArray = new int[15];
for (int i = 0; i < IMEINumber.length(); i++) {
IMEINumberArray[i] = (int) (IMEINumber.charAt(i) - 48);
}
boolean isIMEICorrect = checkIMEI(IMEINumberArray);
if (isIMEICorrect) {
System.out.println("IMEI number is correct");
} else {
System.out.println("IMEI number is incorrect");
}
}
private static boolean checkIMEI(int[] imei) {
int sum1 = 0, sum2 = 0, remaining, totalSum;
for (int i = 1; i <= imei.length; i++) {
if (i % 2 == 0) {
int temp = imei[i - 1] * 2;
if (temp > 9) {
remaining = temp % 10;
temp /= 10;
temp += remaining;
sum1 += temp;
} else {
sum1 += imei[i - 1] * 2;
}
}
if (i % 2 == 1) {
sum2 += imei[i - 1];
}
}
totalSum = sum1 + sum2;
if (totalSum % 10 == 0) {
return true;
} else {
return false;
}
}
}