$devtoolkit.sh/glossary/what-is-luhn-algorithm

What is the Luhn Algorithm? — Credit Card Check Digit Explained

Definition

The Luhn algorithm (also called the modulus 10 or mod 10 algorithm) is a simple checksum formula used to validate identification numbers such as credit card numbers, IMEI codes, and National Provider Identifiers. It was created by IBM scientist Hans Peter Luhn in 1954. The algorithm adds a check digit to a number such that the total of all digits, after doubling alternating digits, sums to a multiple of 10. It is designed to catch common data entry errors, not to provide cryptographic security.

How It Works

Starting from the rightmost digit (not the check digit) and moving left, double every second digit. If doubling produces a number greater than 9, subtract 9. Sum all digits (the original ones and the modified doubled ones), including the check digit. If the total modulo 10 equals 0, the number is valid. For example, Visa card numbers always have a check digit calculated so that the Luhn sum equals 0. A single digit error or a transposition of two non-identical adjacent digits will always fail the Luhn check.

Common Use Cases

  • Client-side validation of credit card numbers before submitting to a payment API
  • Validating IMEI numbers for mobile phone registration
  • Checking National Provider Identifier (NPI) numbers in US healthcare
  • Validating IBANs (which use the related MOD-97 check)
  • Generating valid test credit card numbers for payment integration testing

Example

Validate: 4532015112830366

Step 1: Double alternate digits from right:
4 5 3 2 0 1 5 1 1 2 8 3 0 3 6 6
  10  4   2  10  2  4  6   6  12

Step 2: Subtract 9 where result > 9:
  1   4   2  1   2  4  6   6  3

Step 3: Sum all digits = 50
50 mod 10 = 0 → Valid!

Related Tools

FAQ

Can the Luhn algorithm detect fraud?
No. Luhn only detects accidental single-digit errors and adjacent transpositions. It is trivial to construct a valid Luhn number that is not a real card number. Payment fraud detection requires real-time authorization with the card network, not a checksum.
Why do credit card test numbers like 4111111111111111 always work?
Test numbers like 4111111111111111 (Visa) are designed to pass the Luhn check but fail actual authorization, so they work in test environments. They are published by card networks specifically for testing payment integrations without using real card numbers.
What errors does Luhn NOT catch?
Luhn does not catch: two adjacent digits transposed if they are the same (e.g., swapping two identical digits), twin errors (two identical digits changed to another identical pair), or errors in more than one digit. It also cannot detect the insertion or deletion of the check digit.

Related Terms

/glossary/what-is-luhn-algorithmv1.0.0