Lab 1 Part 2: IMEI Validation

This part of the lab is worth 5 marks.


Every handphone is assigned a unique 15-digit number known as the "International Mobile Equipment Identity" or IMEI. If you own a handphone and would like to check the IMEI, simply key in *#06#. If the number displayed is more than 15 digits, simply take the first 15 digits. This 15-digit sequence comprises of the following:

In particular, the CD can be used to check and determine if the entire IMEI sequence is well-formed. The algorithm used for this purpose is known as the Luhn's algorithm which is also commonly used for checking the validity of 16-digit credit card numbers.


Objective

Write a C program imei.c that repeatedly accepts the following input:

We shall assume that the user obeys instructions and you do not have to explicitly check the validity of the input. For each set of three numbers entered, the TAC and SNR will be used to compute the correct check digit, which will then be compared with CD to determine the validity of the IMEI. The program terminates as soon as a negative number is entered for any of the three input.

The algorithm for determining the check digit is given in the next section. The skeleton code and sample output are also provided. Remember to submit your program before the stipulated deadline for your lab session. The file that you submit must be named imei.c, any other name is not allowed.

Note that you are NOT to use any form of arrays in your program.
However, you may define functions to assist in making your code more modular.


Algorithm

Let the TAC and SNR be jointly represented as d1d2d3d4d5d6d7d8d9d10d11d12d13d14 where digits d1..d8 form the TAC, while digits d9..d14 form the SNR.

Consider the example:

TAC SNR
d1 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 d12 d13 d14
12345678 123456
  1. Double the values of the even labelled digits d2, d4, ... d14. Odd labelled digits remain the same.
    For the above example,

    TAC SNR
    d1 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 d12 d13 d14
    1438512716 1438512

  2. Add together the individual single digits of all the numbers obtained in step 1.
    For the above example,
    1 + 4 + 3 + 8 + 5 + (1 + 2) + 7 + (1 + 6) + 1 + 4 + 3 + 8 + 5 + (1 + 2) = 62
  3. If the number obtained in step 2 ends in 0, then set the check digit to be 0. If the number obtained in step 2 does not end in 0, then set the check digit to be that number subtracted from the next higher number which does end in 0. (You might want to re-formulate this step in another way)
    For the above example, since 62 does not end with a 0, take
    70 - 62 = 8
    so the correct check digit should be 8.


Program Skeleton

#include <stdio.h>

/* Function to read input from the user. Do not modify */
int readInput(char *prompt)
{
   int value;

   printf("%s (-ve to exit): ", prompt);
   scanf("%d", &value);
   return value;
}

/** Include other functions if you feel it is necessary...
    Marks will NOT be deducted even if you do not define functions **/


/* Main function */
int main()
{
   int TAC, SNR, CD;
   /** Declare other variables here */

   while ( ( (TAC = readInput("Enter the 8-digit TAC")) >= 0) &&
           ( (SNR = readInput("Enter the 6-digit SNR")) >= 0) &&
           ( (CD = readInput("Enter the 1-digit CD ")) >= 0) )
   {
      /** You need only modify the loop body to achieve your task **/
      printf("The IMEI entered is %d-%d-%d\n\n", TAC, SNR, CD);
   }

   /* Terminating Program */
   printf("\nProgram Terminated...\n");
   return 0;
}

Sample Output

The following is a sample session. User input is denoted in bold.

Enter the 8-digit TAC (-ve to exit): 12345678
Enter the 6-digit SNR (-ve to exit): 123456
Enter the 1-digit CD  (-ve to exit): 8
This is a valid IMEI.

Enter the 8-digit TAC (-ve to exit): 12345678
Enter the 6-digit SNR (-ve to exit): 123456
Enter the 1-digit CD  (-ve to exit): 0

This is an INVALID IMEI!

Enter the 8-digit TAC (-ve to exit): 35007752
Enter the 6-digit SNR (-ve to exit): 323751
Enter the 1-digit CD  (-ve to exit): 3

This is a valid IMEI.

Enter the 8-digit TAC (-ve to exit): 20450035
Enter the 6-digit SNR (-ve to exit): 385709
Enter the 1-digit CD  (-ve to exit): 0

This is a valid IMEI.

Enter the 8-digit TAC (-ve to exit): 12345678
Enter the 6-digit SNR (-ve to exit): -1

Program Terminated...