IMEI Check Digit Program in Java and Python
IMEI check digit program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.
Question:
Write a program to input the first 14 digits of an IMEI number and find the 15th check digit. The check digit should make the complete IMEI number valid according to the IMEI/Luhn rule.
Algorithm:
Step 1: Start.
Step 2: Accept the first 14 digits as a string.
Step 3: If the length is not 14 or a character is not a digit, display invalid input.
Step 4: Initialize sum as 0.
Step 5: Read the 14 digits from left to right.
Step 6: Double alternate digits according to the IMEI rule and add the sum of digits of the doubled value.
Step 7: Find the digit which makes the total divisible by 10.
Step 8: Display the check digit and the complete IMEI number.
Step 9: Use the loop index to decide whether the current digit must be doubled.
Step 10: When a doubled value is two digits, add its digit sum to the running total.
Step 11: Use the total remainder to calculate the missing check digit.
Step 12: Stop.
Explanation:
This program finds the missing 15th digit of an IMEI number. The first 14 digits are accepted as a string so that the exact number of digits can be checked before doing the calculation.
The same Luhn rule used for IMEI validation is applied to the first 14 digits. Alternate digits are doubled, and if the doubled value becomes two digits, sumDig() adds its digits before adding it to the total.
After processing the 14 digits, the program checks the remainder obtained by dividing the total by 10. If the remainder is 0, the required check digit is also 0 because the number is already divisible by 10.
If the remainder is not 0, the check digit is calculated as 10 - remainder. This is the smallest single digit that can be added to make the complete IMEI total divisible by 10.
The check digit is not guessed by trial in the final formula. The remainder tells how far the current sum is from the next multiple of 10, so 10 - remainder gives the required final digit.
The loop index is important because only alternate positions are doubled. The character at each position is converted to a digit, processed according to its index, and added to the running counter called the total.
Java Program:
/**
* 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);
}
}
}Equivalent Python Program:
# 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 first 14 digits of IMEI number: ")
if len(imei) != 14:
print("Invalid Input")
else:
valid_input = True
total = 0
for i in range(14):
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")
else:
if total % 10 == 0:
check_digit = 0
else:
check_digit = 10 - (total % 10)
print("Check digit =", check_digit)
print("Complete IMEI number =", imei + str(check_digit))Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.