Friday , 15 December 2017
Home » Class 12 » Java Program to check for Valid IMEI Number

Java Program to check for Valid IMEI Number

Question:

The International Mobile Station Equipment Identity or IMEI is a number, usually unique, to identify mobile phones, as well as some satellite phones. It is usually found printed inside the battery compartment of the phone.

The IMEI number is used by a GSM network to identify valid devices and therefore can be used for stopping a stolen phone from accessing that network.

The IMEI (15 decimal digits: 14 digits plus a check digit) includes information on the origin, model, and serial number of the device.

See: Java Program to find check digit of an IMEI Number

The IMEI is validated in three steps:

  1. Starting from the right, double every other digit (e.g., 7 becomes 14).
  2. Sum the digits (e.g., 14 → 1 + 4).
  3. Check if the sum is divisible by 10.

For Example:

If input is IMEI  = 490154203237518

imei number java

Since, 60 is divisible by 10, hence the given IMEI number is Valid.

Design a program to accept a fifteen digit number from the user and check whether it is a valid IMEI number or not. For an invalid input, display an appropriate message.

Programming Code:

/**
* The class IMEI inputs a 15 digit number and checks whether it is a valid IMEI or not
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class IMEI
{
    int sumDig(int n) // Function for finding and returning sum of digits of a number
    {
        int a = 0;
        while(n>0)
        {
            a = a + n%10;
            n = n/10;
        }
        return a;
    }
    
    public static void main(String args[])throws IOException
    {
        IMEI ob = new IMEI();
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        
        System.out.print("Enter a 15 digit IMEI code : ");
        long n = Long.parseLong(br.readLine()); // 15 digits cannot be stored in 'int' data type
        
        String s = Long.toString(n); // Converting the number into String for finding length
        int l = s.length();
        
        if(l!=15) // If length is not 15 then IMEI is Invalid
            System.out.println("Output : Invalid Input");
        else
        {
            int d = 0, sum = 0;
            for(int i=15; i>=1; i--)
            {
                d = (int)(n%10);
                
                if(i%2 == 0)
                {
                    d = 2*d; // Doubling every alternate digit
                }

                sum = sum + ob.sumDig(d); // Finding sum of the digits
                
                n = n/10;
            }
            
            System.out.println("Output : Sum = "+sum);
            
            if(sum%10==0)
                System.out.println("Valid IMEI Code");
            else
                System.out.println("Invalid IMEI Code");
        }
    }
}

Output:

1. Enter a 15 digit IMEI code : 654122487458946
Output : Sum = 80
Valid IMEI Code

2. Enter a 15 digit IMEI code : 799273987135461
Output : Sum = 79
Invalid IMEI Code

3. Enter a 15 digit IMEI code : 79927398713
Output : Invalid Input

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

4 comments

  1. In this program if the length was needed to be found then the IMEI could have been inputted as a string directly.
    It is useless to input it as long and on the very next line to convert it into String.

    • Hello Tamoghna,
      In the program, ONLY length was not to be found. We need the number form also so that we can take out its digits one by one.
      The number was converted to String, just to ease our task of finding the number of digits present in it.
      Even if it had been inputted in String format, after finding the length, we would have needed to convert it into a number.

  2. This site helps me a lot in writing program

Leave a Reply to vyas Cancel reply

Your email address will not be published. Required fields are marked *