6 June, 2021, 6:11 am

Luhn algorithm’s usage in Credit Cards

3 min read

The Luhn algorithm or Luhn formula, also known as the “modulus 10” or “mod 10” algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, Etc. Most credit cards and many government identification numbers use the algorithm as a simple method of distinguishing valid numbers from mistyped or otherwise incorrect numbers.

Most credit cards contain a check digit, which is the digit at the end of the credit card number. The first part of the credit-card number identifies the type of credit card (Visa, MasterCard, American Express, Discover etc.), and the middle digits identify the bank and customer.

Visa — 4

MasterCard — 5

American Express — 3

Discover — 6

Most of the companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test:

Here’s how the algorithm works for verifying credit cards;

The math is quite simple behind the algorithm.

1). Reverse the order of the digits in the number.

2). Take the first, third, … and every other odd digit in the reversed digits and sum them to form the partial sum s1.

3). Taking the second, fourth … and every other even digit in the reversed digits:

4).Multiply each digit by two and sum the digits if the answer is greater than nine to form partial sums for the even digits

5).Sum the partial sums of the even digits to form s2

If s1 + s2 ends in zero then the original number is in the form of a valid credit card number as verified by the Luhn test.

For example, if the trial number is 5105 1051 0510 5100 lets apply the luhn algorithm

Reverse the digits : 0 0 1 5 0 1 5 0 1 5 0 1 5 0 1 5

Sum the odd digits : 0 + 1 +0 +5+1+0+5+1 = 13 = s1

even digits : 0,5,1,0,5,1,0,5

twice the even digits : 0, 10 , 2, 0, 10,2,0,10

Since 10 is > 9 sum of the digit of 10 is 1+0 = 1

So the sum of the last = 0 + 1 + 2+0+1+2+0+1 = 7 = s2

s1 + s2 = 20 which ends in zero which means that 5105 1051 0510 5100 passes the Luhn test.

public bool IsPassesLuhnTest(string cardNumber) 
{ 
  //Clean the card number- remove dashes and spaces 
  cardNumber = cardNumber.Replace("-", "").Replace(" ", 
  ""); 
  //Convert card number into digits array 
  int[] digits = new int[cardNumber.Length]; 
  for (int len = 0; len < cardNumber.Length; len++) 
    {       digits[len] = Int32.Parse(cardNumber.Substring(len, 1));
 
    }     //Luhn Algorithm     int sum = 0; 
    bool alt = false; 
    for (int i = digits.Length - 1; i >= 0; i--) 
       { 
          int curDigit = digits[i]; 
          if (alt) 
             { 
                 curDigit *= 2; 
                 if (curDigit > 9) 
                   {  
                      curDigit -= 9; 
                   } 
             } 
             sum += curDigit; 
             alt = !alt; 
        } 
        //If Mod 10 equals 0, the test is ok and it will return true    return sum % 10 == 0; 
 }

Thanks for reading! Follow and clap for more industry trends and news.

You may also like

enterprise software-blog
by Orvero Labs | 12 min read
6 key enterprise software development best practices

So, you want to build enterprise software for your business but you don’t know how? And it...

Read more
MVP development
by Orvero Labs | 15 min read
MVP development for startups: step-by-step guide

Creating a minimum viable product (MVP) is crucial for validating your idea and demonstrating its...

Read more
development vendor
by Orvero Labs | 9 min read
How to choose the right software development vendor

Entering into software development presents a significant opportunity for your business. However, it’s crucial to...

Read more