-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIMEI.java
More file actions
38 lines (35 loc) · 1.03 KB
/
Copy pathIMEI.java
File metadata and controls
38 lines (35 loc) · 1.03 KB
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
package basic;
import java.util.Scanner;
class IMEI {
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
System.out.println("Enter first 14 digits of an basic.IMEI no.");
String str = ob.nextLine();
int len = str.length();
if (len != 14) {
System.out.println("Invalid Input");
System.exit(0);
}
int sum = 0;
for (int i = 0; i < 14; i++) {
char ch = str.charAt(i);
int temp = (int) ch - 48;
if (i % 2 != 0)//Doubling every alternate digits
{
temp = temp * 2;
}
sum = sum + digitSum(temp);
}
System.out.println("Sum of digits:" + sum);
int checkDigit = (sum * 9) % 10; //Finding check digit
System.out.println("Check Digit:" + checkDigit);
}
public static int digitSum(int n) {
int s = 0;
while (n != 0) {
s += n % 10;
n = n / 10;
}
return s;
}
}