DEV Community

Cover image for Credit Card Validator using Luhn Algorithm 💳
Rakshambika
Rakshambika

Posted on

Credit Card Validator using Luhn Algorithm 💳

What is the Luhn Algorithm?

The Luhn Algorithm is a checksum algorithm used to validate identification numbers, such as credit card numbers.

It helps check whether a card number follows a valid mathematical pattern.

The Luhn algorithm only verifies the mathematical structure of a number. It does not confirm whether a credit card is genuine, active, or authorized for transactions.

How Does the Luhn Algorithm Work?

The algorithm follows these steps:

  • Start reading the number from the right side.
  • Skip the last digit (check digit).
  • Double every second digit.

  • If the doubled value is greater than 9, add its individual digits.

e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6, to get a single digit number.

  • Add all the resulting digits.

  • Check whether the total is divisible by 10.

If the total is divisible by 10, the number passes the Luhn check.


Example :

Let's take this number:

79927398713

We process the digits from right to left.

Original digit Operation Result
3 Skip 3
1 1 × 2 2
7 Skip 7
8 8 × 2 = 16 → 1 + 6 7
9 Skip 9
3 3 × 2 6
7 Skip 7
2 2 × 2 4
9 Skip 9
9 9 × 2 = 18 → 1 + 8 9
7 Skip 7

Total = 70

Since 70 is divisible by 10, this number passes the Luhn algorithm check.

Python Implementation :


print("********************************")
print("Welcome to Credit Card Validator")
print("********************************")


credit_number = int(input("Enter your credit card number :"))


num = credit_number
total_sum=0
count = 1

while num > 0:
    digit = num % 10
    num = num // 10

    if count % 2 == 0:
       digit = digit*2

    if digit > 9 :
       sum=0

       first = digit % 10

       sum += first

       digit = digit // 10

       sum += digit
       digit = sum

    total_sum += digit
    count += 1


print("Total Sum: " ,total_sum)



if(total_sum % 10 == 0):
  print(credit_number ," is a valid card")

else:
  print(credit_number , " is not a valid card")



Enter fullscreen mode Exit fullscreen mode

Output:

Example for Valid Card :

********************************
Welcome to Credit Card Validator
********************************
Enter your credit card number :79927398713
Total Sum:  70
79927398713  is a valid card

Enter fullscreen mode Exit fullscreen mode

Example for Invalid card :


********************************
Welcome to Credit Card Validator
********************************
Enter your credit card number :4552720412345678
Total Sum:  61
4552720412345678  is not a valid card

Enter fullscreen mode Exit fullscreen mode

How do different industries use the Luhn algorithm?

The Luhn algorithm is used across multiple industries to verify critical numerical data, prevent accidental input errors, and enhance fraud protection.

Online shopping:

  • When you make a purchase online, the payment system uses the Luhn algorithm to verify that you entered a valid credit card number before processing the transaction.
  • This helps reduce declined payments due to typos or incorrect numbers.

Banking systems:

  • Banks and credit card companies use the Luhn algorithm to validate account numbers and routing numbers to ensure that funds are transferred to the correct accounts.
  • This ensures that funds are transferred to valid financial accounts before electronic funds transfers (EFT) or payments are processed.

Utility companies:

  • Utility service providers often use the Luhn algorithm to validate customer account numbers.
  • This ensures that billing and payment information is associated with the correct accounts, reducing billing errors and customer service issues.

Ticketing systems:

  • Airlines, railways, and other transportation companies use the Luhn algorithm to validate booking reference numbers or ticket numbers, ensuring that data is entered correctly and reducing the risk of ticket fraud.

References :

  1. Stripe – How to Use the Luhn Algorithm

  2. GeeksforGeeks – Luhn Algorithm

Top comments (0)