Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IMEI (International Mobile Equipment Identifier) Every mobile phone operating in

ID: 3675665 • Letter: I

Question

IMEI (International Mobile Equipment Identifier) Every mobile phone operating in wireless networks is characterized by a unique identifier of 15 digits , called IMEI ( https://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity ) A method to check if the device is really made by the official manufacturer is to compare the IMEI’s last digit , called Luhn digit , with a check digit. If the Luhn digit is equal to the check digit , the device is most probably authentic. Otherwise, it is not authentic for sure.

Write a c program that calculates the check digit:

1) First, we calculate the SUM of the first IMEI’s 14 digits by adding

    a) the digits in the odd positions (meaning even index for the array in C)

    b) the double of the digits in the even positions. But if the double of the digit is a two - digit number, we add each digit separately. For example, suppose that the value of the digit is 8, its double is 16. We therefore add to the SUM the result of 1+6 = 7 (not 16).

2) If the last digit of the calculated SUM is 0, that is the check digit. If not, we subtract the last digit of the calculated SUM from 10 and that is the check digit.

For example: let’s check the IMEI 357683036257378 . Note: the last digit of the given IMEI above is 8 which is the Luhn digit. Apply the above algorithm (by hand) to the first 14 digits , we get

3 + ( 2 x5) +7 + ( 2 x6) + 8 + ( 2 x3) + 0 + ( 2 x3) + 6 + ( 2 x2) + 5+ ( 2 x7) + 3+ ( 2 x7)

3 + (10) +7 + (12) + 8 + (6) + 0 + (6) + 6 + (4) + 5+ (14) + 3+ (14)

3 + (1+0) +7 + (1+2) + 8 + (6) + 0 + (6) + 6 + (4) + 5+ (1+4) + 3+ (1+4)

3 + (1) +7 + (3) + 8 + (6) + 0 + (6) + 6 + (4) + 5+ (5) + 3+ (5) = 62

Then the check digit = 10 – 2 = 8. Since the check digit (=8) is equal to the Luhn digit (= 8 ), this IMEI is valid.

Explanation / Answer

#include <stdio.h>

int main()
{
   char ch, chk_dig;
   int i, sum, temp;

   sum = 0;  
   printf("Enter IMEI (15 digits): ");
   for(i = 1; i < 15; i++) /* Read the first 14 IMEI’s digits. */
   {
       ch = getchar();
       if((i & 1) == 1) /* Check if the digit’s position is odd. */
           sum += ch-'0'; /* The expression ch-'0' calculates the numerical value of the digit character. */
       else
       {
           temp = 2*(ch-'0');
           if(temp >= 10)
               temp = (temp/10) + (temp%10); /* If the digit’s doubling produces a two-digit number we calculate the sum of these digits. */
           sum += temp;
       }  
   }
   ch = getchar(); /* Read the IMEI’s last digit, that is the Luhn digit. */
   ch = ch-'0';

   chk_dig = sum % 10;
   if(chk_dig != 0)
       chk_dig = 10-chk_dig;

   if(ch == chk_dig)
       printf("*** Valid IMEI *** ");
   else
       printf("*** Invalid IMEI *** ");
   return 0;
}

sample output

Enter IMEI (15 digits): 123456789101213145                                                                                                                  
*** Invalid IMEI ***