This article will explain how to check if a given IMEI number is valid using java program with explanation and output.
What is IMEI number
IMEI stands for International Mobile Equipment Identity is a 15 digit number associated with a mobile phone. Each mobile phone comes with an IMEI number which is generally unique.
The IMEI number is printed inside the phone and can also be displayed on its screen by dialing *#06#.
IMEI number is used for tracking the phone in case of stolen or criminal activities.
Mobile network operators can block a phone’s usage using its IMEI number.
How to check if IMEI is valid

There are two criteria for being an IMEI number as valid or invalid.

  1. Its length should be exactly 15 digits.
  2. Second criteria consists of adding up all the digits but its not simple addition. Go through the below steps:
    • Start from the right of the number(that is, unit digit). Double every alternate digit and add it to sum. That is, when you start from right, double 2nd, 4th, 6th digits and so on. While the 1st, 3rd, 5th digits are added as is.
    • If double of a digit comes out to be a two-digit number, then add those digits. Example, if 8 * 2 = 16, then 7(=6 + 1) will be added to the sum.
    • If the final sum is completely divisible by 10, then the entered IMEI number is valid else not.

Java Program

Program to check if a given IMEI number is valid or not is given below. It is based on the algorithm outlined above.

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class IMEIValidator { 
  public static void main(String[] args) throws IOException { 
    BufferedReader inputReader = null; 
    System.out.println("Input 15 digit IMEI number"); 
    try { 
      // initialize input reader 
      inputReader = new BufferedReader(new InputStreamReader(System.in)); 
      // read IMEI number from user input 
      String imeiStr = inputReader.readLine(); 
      // convert it to a long 
      long imei = Long.parseLong(imeiStr); 
      // check the length which is the first criteria 
      if (imeiStr.length() != 15) { 
        System.out.println("Invalid IMEI number: does not contain 15 digits"); 
        return; 
      }  
      // variable to hold the sum 
      int sum = 0; 
      // start from the rightmost digit 
      for (int i = 0; i < imeiStr.length(); i++) { 
        int digit = (int) (imei % 10); 
        // check if the digit is second, fourth and so on. These digits 
        // will be at odd positions since number positions start from 0 
        if (i % 2 != 0) { 
          digit = 2 * digit; 
        } 
        // get sum of digits and add it to sum 
        sum += getSumOfDigits(digit); 
        // remove the last digit of the number 
        imei = imei / 10; 
      } 
      // check if sum is divisible by 10 
      if (sum % 10 == 0) { 
        System.out.println("IMEI is valid"); 
      } else { 
        System.out.println("IMEI not valid");
      } 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } finally { 
      // close input reader 
      if (inputReader != null) { 
        inputReader.close(); 
      } 
    } 
  } 

  /** 
   * Takes a number and adds up its digits. Useful when the number is of 
   * multiple digits 
   * @param number 
   * @return 
   */ 
  static int getSumOfDigits(int number) { 
    // hold sum of digits 
    int sumOfDigits = 0; 
    while (number > 0) { 
      // get the last digit 
      int digit = number % 10; 
      // add it to sum 
      sumOfDigits += digit; 
      // remove the last digit 
      number = number / 10; 
    } 
    return sumOfDigits; 
  } 
}

Above program iterates over the number starting from right. You can also iterate it starting from the left.
In that case the loop will iterate as for (int i = imeiStr.length(); i >= 1; i--) and instead of checking for an odd digit, you need to check for an even digit as if (i % 2 != 0).
This is because when you start from left, every second digit will be at even position.

While checking for alternate digit, keep in mind that the digit index start from 0 in java.

Hope the article was useful.