Luhn algorithm’s usage in Credit Cards

Orvero Labs
3 min readJun 6, 2021

--

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 news.

--

--

Orvero Labs
Orvero Labs

Written by Orvero Labs

Orvero is an information technology services company founded in Sri Lanka. The company provides IT consulting, business consulting, systems implementation

No responses yet