SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Valid IMEI Number Program in Java and Python

18 February 2014

Valid IMEI number program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to accept a 15 digit IMEI number and check whether it is a valid IMEI number or not. Starting from the right, double every alternate digit excluding the check digit. If the doubled value has two digits, add those digits. The number is valid when the total is divisible by 10.

INPUT: Enter a 15 digit IMEI number: 490154203237518 OUTPUT: Valid IMEI Number

Algorithm:

Step 1: Start.

Step 2: Accept the IMEI code as a string.

Step 3: If the length is not 15, display INVALID INPUT and stop.

Step 4: Initialize sum to 0.

Step 5: Repeat for index i from 0 to 14.

Step 6: If the current character is not a digit, display INVALID INPUT and stop.

Step 7: Convert the character digit into its integer value.

Step 8: If i is odd, double the digit and add the sum of digits of the doubled value to sum.

Step 9: If i is even, add the digit directly to sum.

Step 10: After all digits are processed, check whether sum is divisible by 10.

Step 11: Display valid IMEI if divisible by 10; otherwise display invalid IMEI.

Step 12: Stop.

Explanation:

The IMEI validation is based on the Luhn algorithm. The IMEI is accepted as a string so that the program can check its exact length and also preserve leading zeroes if any.

The program first rejects any input that is not exactly 15 characters long or contains a non-digit character. This prevents incorrect numeric conversion and keeps the validation strict.

For a 15 digit IMEI, alternate digits are doubled starting from the second digit from the left. If doubling gives a two digit number, the method sumDig() adds those digits. For example, 8 doubled becomes 16, and 1 + 6 is added to the total.

The variable sum stores the running total of unchanged digits and processed doubled digits. If sum % 10 is 0 at the end, the check digit is correct and the IMEI is valid; otherwise, it is invalid.

IMEI validation uses the Luhn-style check digit process. The program processes the digits in specific positions, doubling alternate digits according to the rule. If doubling produces a two-digit number, its digits are added so that the contribution remains a single digit sum. All contributions are added to form a total. A valid IMEI has a total divisible by 10. The important part is position control: the program must double the correct alternate digits and keep the original digit order in mind while calculating the check sum.

Java Program:

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

import java.util.Scanner;
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[])
    {
        IMEI ob = new IMEI();
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a 15 digit IMEI code : ");
        long n = sc.nextLong(); // 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");
        }
    }
}

Equivalent Python Program:

Python
# Read the code as a string so each digit/character position can be checked.
# Position-based loop logic applies the required checksum calculation.
# Use the final remainder/check value to decide and display validity.

imei = input("Enter a 15 digit IMEI number: ")

if len(imei) != 15:
    print("Invalid Input")
else:
    valid_input = True
    total = 0

    for i in range(15):
        ch = imei[i]
        if ch < '0' or ch > '9':
            valid_input = False
            break

        digit = ord(ch) - ord('0')
        if i % 2 == 1:
            value = digit * 2
            total = total + value % 10 + value // 10
        else:
            total = total + digit

    if valid_input == False:
        print("Invalid Input")
    elif total % 10 == 0:
        print("Valid IMEI Number")
    else:
        print("Invalid IMEI Number")

Output:

INPUT: Enter a 15 digit IMEI number: 490154203237518 OUTPUT: Valid IMEI Number

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →