top of page

Validating Credit Card Information - Luhn’s Algorithm.

Credit card numbers typically have a structure to them: American Express uses 15-digit numbers all starting with 34 or 37, MasterCard uses 16-digit numbers starting with 51, 52, 53, 54, or 55 , and Visa uses 13 and 16-digit numbers all beginning with 4. These values are decimal numbers (0 through 9), not binary, which means, for instance, that American Express could print as many as 10^(15) = 1,000,000,000,000,000 unique cards!

Credit cards rely on a built in "checksum", this is a mathematical relationship enabling computers to detect transpositions, resulting in fraudulent numbers, without having to rely on a slow database query. Most cards use an algorithm invented by Hans Peter Luhn.

Based on Luhn’s algorithm, the process to validate that a credit card number is syntactically valid is as follows:

  • Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products' digits together.

  • Add the resulting sum to the remaining sum of the digits that were not multiplied by 2.

  • If the total sum's last digit is 0, the number is valid!

For example, the AMEX card #: 378282246310005:

  • Obtain all secondary values: 3 7 8 2 8 2 2 4 6 3 1 0 0 0 5

  • Double all second values: 7•2 + 2•2 + 2•2 + 4•2 + 3•2 + 0•2 + 0•2

  • This results in: 14 + 4 + 4 + 8 + 6 + 0 + 0

  • Sum up the remaining digits: 27 + 3 + 8 + 8 + 2 + 6 + 1 + 0 + 5 = 60

  • 60 % 10 = 0, a valid card!


Note: This program will be written in C

Let's first outline the variables required, we will need to store:

  • A record of the CC#, and a secondary variable to be used when operating on CC#

  • The first and second digit in CC#

  • The number of digits in the CC

  • A const for the min & max possible lengths of CC#

  • Arrays to store both the even and odd CC digits

  • The number of digits in CC#

  • Separate variables for the max number of even and odd digits in CC#

  • Total sum, recording the even Sum + all other digits

We will then need to prompt the user for a credit card number. At this stage we can already filter out invalid card values which exceed the max possible CC length (Mastercard & Visa have the longest @ 16 digits).

Obtaining the even and odd digits in the CC number is fairly simple. Take for instance, the process to calculate the sum of all the 2nd digits in the card. We begin by recording the quotient of the CC# divided by a value of 10, resulting in a temporary CC# simply missing the last value. We then iterate through the CC#, assigning every second digit to an array. With every iteration, we take the modulus of tempCC# by a value of 2 (returning the second last digit), then dividing the tempCC by 100 (thus throwing away the last 2 digits). As per Luhn's algorithm, we double all elements of the array by 2, then sum all elements together.

Once we have obtained a final sum, If the total modulo 10 is equal to 0, then the number is valid according to the Luhn formula; else it is not valid. Upon validation, we can then output the corresponding card type by simply checking the first and second digits of the CC# against known starting values. I've opted to use a nested if/else statement here, although a switch statement may prove more concise. A complete solution can be found on my GitHub.

Recent Posts
Search By Tags
bottom of page