# Checking IMEI Number Using Java Programs

IMEI number (International Mobile Equipment Identity) is a unique number used to identify mobile phones. IMEI is used by mobile networks to uniquely identify a device and hence the IMEI number is used to block stolen devices.

IMEI number is composed of 15 decimal digits including a check digit. The last digit in the IMEI number is a check digit computed using Luhn algorithm. The check digit is used to protect against data entry errors in IMEI numbers when entered in devices.

Following algorithm is used to find the check digit in an IMEI number.

- Given the 14 digits of IMEI number, start from the right.
- Find the double of every other digit.
- Find the sum of all digits in the number using doubled numbers for every other digit as computed above.
- Multiply the sum by 9 and then divide by 10. The remainder is the check digit.

Here is an example illustrating computation of IMEI check digit,

- 14 digits of an IMEI number = 42015420343761
- Sum digits (double of every other digit from right) = 4+4+0+2+5+8+2+0+3+8+3+14+6+2 = 4+4+0+2+5+8+2+0+3+8+3+5+6+2 = 52.
- Multiply with 9 = 52 * 9 = 468
- Divide by 10 and find the remainder = 468 % 10 = 8
- The check digit is 8. This is the number which when added with 52 makes it divisible by 10.
- The full IMEI number is 420154203437618.

## Problem: Find the check digit from first 14 digits of an IMEI number using Java

The following Java program computes the last check digit of an IMEI number given the first 14 digits. This program can be used to compute and verify IMEI numbers.

import java.util.Scanner; // IMEI Java check digit generator example source code public class IMEICheckDigitGenerator { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Please enter first 14 digits of IMEI number: "); String input = s.nextLine(); int checkDigit = getCheckDigit(input); System.out.println("Check digit: "+checkDigit); System.out.println("IMEI Number: "+input+checkDigit); s.close(); } // Returns check digit for 14 digit IMEI prefix public static int getCheckDigit(String imeiPrefix) { int sum = 0; for(int i = 13;i>=0;i=i-1) { String sDigit = imeiPrefix.substring(i,i+1); int digit = Integer.valueOf(sDigit); if(i%2==0) { sum = sum + digit; }else { sum = sum + sumOfDigits(digit*2); } } sum = sum * 9; return sum%10; // Return check digit } // Calculate sum of digits for a number public static int sumOfDigits(int number) { int sum=0; while(number > 0) { sum += number%10; number = number/10; } return sum; } }

Following is a sample output of the above program,

Please enter first 14 digits of IMEI number: 42015420343761 Check digit: 8 IMEI Number: 420154203437618

## Problem: Check whether the given 15 digit number is a valid IMEI number using Java

The following java program verifies whether a given IMEI number is valid or not. It uses the last check digit for validation.

import java.util.Scanner; // Java program to check whether an IMEI number is valid public class IMEIValidatorInJava { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Please enter a 15 digit IMEI number: "); String input = s.nextLine(); int computedCheckDigit = getCheckDigit(input.substring(0,14)); int checkDigitInSource = Integer.valueOf(input.substring(14)); if(computedCheckDigit == checkDigitInSource) { System.out.println(input+" is a valid IMEI number!"); }else { System.out.println(input+" is NOT a valid IMEI number!"); System.out.println("Check digit computed: "+computedCheckDigit); } s.close(); } // Returns check digit for 14 digit IMEI prefix public static int getCheckDigit(String imeiPrefix) { int sum = 0; for(int i = 13;i>=0;i=i-1) { String sDigit = imeiPrefix.substring(i,i+1); int digit = Integer.valueOf(sDigit); if(i%2==0) { sum = sum + digit; }else { sum = sum + sumOfDigits(digit*2); } } sum = sum * 9; return sum%10; // Return check digit } // Calculate sum of digits for a number public static int sumOfDigits(int number) { int sum=0; while(number > 0) { sum += number%10; number = number/10; } return sum; } }

Following is a sample output from the program,

Please enter a 15 digit IMEI number: 914859533683732 914859533683732 is NOT a valid IMEI number! Check digit computed: 0