Thursday , 25 May 2017
Home » Class 12 » Java Program to find check digit of an IMEI Number

Java Program to find check digit of an IMEI Number

Question:

Write a program in Java to input the first 14 digits of an IMEI number and find the check (last) digit of it.

See: Java Program to check for Valid IMEI Number

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

The check digit (x) is obtained by computing the sum of digits then computing 9 times that value modulo 10.

imei check digit

In algorithm form:

  1. Compute the sum of the digits (52 in this case).
  2. Multiply the sum by 9 (9*52 = 468).
  3. Divide the result by 10 and note down the remainder (468 % 10)
  4. The last digit, 8, is the check digit.

Programming Code:

/**
* The class IMEI_CheckDig inputs the first 14 digits of an IMEI number
* and finds the last check digit of the IMEI code
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class IMEI_CheckDig
{
    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_CheckDig ob = new IMEI_CheckDig();
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        
        System.out.print("Enter first 14 digits of IMEI code : ");
        long n = Long.parseLong(br.readLine()); // 14 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!=14) // If length is not 14 then IMEI is Invalid
            System.out.println("Output : Invalid Input");
        else
        {
            int d = 0, sum = 0;
            for(int i=14; 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("Sum = "+sum);

            int dig = (9*sum)%10; // Finding the check digit
            System.out.println("Output : The check digit = "+dig);
            
        }
    }
}

Output:

Enter first 14 digits of IMEI code : 49015420323751
Sum = 52
Output : The check digit = 8

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

5 comments

  1. Please keep on posting such interesting programs….thank you

  2. Sir on executing this program this error is coming and i am not able to remove this please help

    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Long.parseLong(Long.java:403)
    at java.lang.Long.parseLong(Long.java:461)
    at IMEI_CheckDig.main(IMEI_CheckDig.java:21)

  3. SHAILENDRA KUMAR YADAV

    very useful study materials for board exams;

    thank you

Leave a Reply to sarvapriyatrip@gmail.com Cancel reply

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