← All resources

Write a program to input a number and check whether it is a Valid IMEI number or not

The International Mobile Station Equipment Identity or IMEI is a number, usually unique, to identify 3GPP (i.e., GSM, UMTS and LTE) and iDEN mobile phones. It is usually found printed inside the battery compartment of the phone, but can also be displayed on-screen on most phones by entering *#06# on the dialpad, or alongside other system information in the settings menu on smartphone operating systems.
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. For example, if a mobile phone is stolen, the owner can call his or her network provider and instruct them to “blacklist” the phone using its IMEI number. This renders the phone useless on that network and sometimes other networks too, whether or not the phone’s SIM is changed.
The IMEI has 15 decimal digits: 14 digits plus a check digit
To check whether a number is a valid IMEI number.
Every second digit is doubled and their sum of digits is added to the sum of the rest of the number.
If this sum is divisible by 10 then the number is a valid IMEI number

Output

Enter a 15 digit IMEI code : 490154203237518 Output : Sum = 60 The number is a Valid IMEI Code

import java.io.*;
public class IMEI
{
    int sum_dig(int n) // Function for finding and returning sum of digits of a number
    {
        int s = 0, d;
        while(n!=0)
        {
            d = n%10; //Extracting the last digit
            s = s + d; //Adding the digit to the sum
            n = n/10; //Deleting the last digit
        }
        return s; //Returning the sum
    }

    public static void main()throws IOException
    {
        IMEI obj = new IMEI(); //Creating object to call non static member from a static function
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter a 15 digit IMEI code : ");
        long n = Long.parseLong(r.readLine()); // 15 digits cannot be stored in 'int' data type

        String x = ""+n; // Converting the number into String for finding length

        if(x.length()!=15) // If length is not 15 then IMEI is Invalid
        {
            System.out.println("Invalid Input");
            return; //Terminating the function
        }
        int d = 0, sum = 0, i;
        for(i=0; i<15; i++)
        {
            d = x.charAt(i)-48; //Extracting Character and Coverting to Integer

            if(i%2 != 0) //Since i is denoting index
            {
                d = 2*d; // Doubling every alternate digit
            }
            sum = sum + obj.sum_dig(d); // Finding sum of the digits
        }
        System.out.println("Output : Sum = "+sum);
        if(sum%10==0)
            System.out.println("The number is a Valid IMEI Code");
        else
            System.out.println("The number is an Invalid IMEI Code");
    }
}