Your Card is Valid – Solution

Implementing a written algorithm is something you do frequently as a C language programmer. You’re given a set of directions and your job is to translate that English into computer code — and make that code work.

For this month’s exercise, the challenge was to apply the MOD 10 algorithm to a series of sample credit card numbers and report whether each is valid. As a review, here is the algorithm:

  1. Take all the digits in the card number, except for the final digit (the check digit) and double every other digit.
  2. If a digit is greater than 9 (two-digits wide), add the digits. So the value 14 (double 7) changes into 5.
  3. Add the digits together.
  4. Multiply the result by 9.
  5. Compare the last digit of the result with the check digit.

For my solution, I used two for loops. The first processes elements of the card[] array. A second for loop tallies the digits to get the sum (Step 3). That’s followed by some math to calculate the result (Step 4) and compare it with the check digit (Step 5). A switch-case structure then examines the card number’s first digit to report the card type.

First comes the for loop that tallies the digits:

        /* tally the digits */
        sum = 0;
        for(d=0; d<15; d+=2)
        {
            /* double every odd digit */
            odd = (card[d] - '0') * 2;
            /* Any values greater than 9, sum the digits */
            if( odd > 9)
                odd = odd % 10 + 1;
            /* fetch the even digit */
            even = (card[d+1] - '0');
            /* Add all the digits together except check digit*/
            if( d < 14 )
                sum += odd + even;
            else
                sum += odd;
        }

int variable sum holds the digit tally. It's initialized to zero.

The for loop works through every other (odd) digit; loop variable d is incremented by 2 with d+=2 as the loop step value.

Variable odd holds the result of a doubled digit in the card number. Character value '0' is subtracted from the character digit to obtain the integer value, which is then doubled. If the value of odd is greater than 9, a quick digit-sum is performed to make the results single-digit. The calculation is odd % 10 + 1, which is sneaky, but it works.

The if( d < 14 ) statement tallies the digits in the card number, both the unchanged even digits and the doubled odd digits. This statement ensures that for the last iteration of the loop, only the final odd digit is added to the sum, not the check digit.

After the loop is complete, digit tally is held in variable sum. The remainder of the algorithm is implemented as follows:

        /* multiply the tally by 9 */
        sum *= 9;
        /* compare the last digit of the result to the check digit */
        last_digit = sum % 10;
        check_digit = card[15] - '0';
        if( check_digit == last_digit)
            printf("Valid ");
        else
            printf("Invalid ");

The sum is multiplied by 9, then the last digit is peeled off and stored in the last_digit variable. That value is compared with the final digit of the card number, stored in variable check_digit. The if statement displays the results.

The next chunk of code displays the card type. To view that chunk, click here to examine my full solution.

Here is the program's sample output:

4485113397627032: Valid Visa
6011621789921290: Valid Discover
5542619587695576: Invalid Mastercard
5107425739448074: Valid Mastercard
4556114306308858: Valid Visa

If your programming journey arrived at the same destination, and your code was able to verify the card numbers as shown above, then great! Many solutions exist to solve programming puzzles and mine is only one of them.

Leave a Reply