Guide For School logo Guide For SchoolICSE and ISC Resources

Java Program to find check digit of an IMEI Number

18 February 2014

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


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:

Java
/**
* 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.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;
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[])
    {
        IMEI_CheckDig ob = new IMEI_CheckDig();
        Scanner sc = new Scanner(System.in);
        
        System.out.print("Enter first 14 digits of IMEI code : ");
        long n = sc.nextLong(); // 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

Tags and Categories

Class 12ISC Important ProgramsPractical QuestionsISCComputer Applications