Back

The Hidden Math Behind Your Bank Card: The Luhn Algorithm Explained

Every time you type a card number online, something checks it before your bank even sees it. That something is a 65-year-old math formula. Here is how it works.

The Hidden Math Behind Your Bank Card: The Luhn Algorithm Explained

You are buying something online. You type your card number. You mistype one digit.

The website immediately says: “Invalid card number.”

Your bank was never contacted. No server checked your account. So how did it know?

The answer is the Luhn Algorithm — a simple math trick invented in 1960 that is quietly hiding inside every bank card number on the planet. Let’s break it down from scratch.


Key Takeaways

Before we get into the details, here is what you will walk away knowing:

  • Every card number has a hidden “self-check” digit built into the last position
  • The Luhn Algorithm catches typos instantly — before any server is contacted
  • It does not check your balance, CVV, or expiry date — only the number format
  • It is used in credit cards, debit cards, phone IMEI numbers, and national ID systems worldwide
  • You can verify any card number yourself with basic math

What Is the Luhn Algorithm?

The Luhn Algorithm is a checksum formula. A checksum is a small piece of extra information added to a number that lets you verify the number has not been entered wrong.

Think of it like this. Imagine you text your friend a phone number and add “the digits should add up to 30” at the end. If your friend adds the digits and gets 28, they know something went wrong. That is basically what Luhn does — but with a more specific math rule.

It is also called the “mod 10” algorithm, because the final check is whether the sum is divisible by 10.

Who Made It

Hans Peter Luhn, a computer scientist at IBM, invented it in 1960. Credit cards were becoming common and people were entering numbers by hand — on paper, over the phone, on early computers. Mistakes were constant.

His solution was elegant: make the card number itself carry the proof that it is correct. No database lookup needed. No server call. Just math.

He filed a patent for it on January 6, 1960 — US Patent No. 2,950,048. He called it “Computer for Verifying Numbers.” Over 65 years later, it runs on billions of devices without a single change.


What the Digits in a Card Number Actually Mean

Most people treat their card number as one long random string. It is not.

4 5 6 7   8 9 0 1   2 3 4 5   6 7 8 9

It is split into three parts:

PartDigitsPurpose
IIN / BINFirst 6Identifies the bank and card network
Account NumberNext 9Your specific account at that bank
Check DigitLast 1The Luhn checksum digit

The first digit alone tells you the network. Cards starting with 4 are Visa. 5 is Mastercard. 3 is Amex. The rest of the first six digits narrow it down to your specific bank.

The last digit — in our example, 9 — is calculated from all the other 15 digits. It is not random. If you change any single digit anywhere in the number, that last digit will no longer match the formula. The system catches it immediately.

Bank card number anatomy — showing the Major Industry Identifier, Issuer Identifier, Account Number, and Check Digit sections labeled on a real card


How the Luhn Algorithm Works — Step by Step

Let’s use this card number: 4567 8901 2345 6789

We will walk through every step exactly as the algorithm does it.

Step 1 — Write Out All the Digits

4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9

Step 2 — Double Every Second Digit From the Right

Start at the second-to-last digit and move left, doubling every other one.

Position from right:  16  15  14  13  12  11  10   9   8   7   6   5   4   3   2   1
Original digit:        4   5   6   7   8   9   0   1   2   3   4   5   6   7   8   9
Double this one?:      Y       Y       Y       Y       Y       Y       Y       Y

One rule to remember: if doubling gives you 10 or more, add the two digits of the result together.

So 6 x 2 = 12, which becomes 1 + 2 = 3. This keeps everything as a single digit.

Step 3 — The Transformation Table

Position (from right)OriginalWhat We DoResult
164x 2 = 88
155no change5
146x 2 = 12, then 1+23
137no change7
128x 2 = 16, then 1+67
119no change9
100x 2 = 00
91no change1
82x 2 = 44
73no change3
64x 2 = 88
55no change5
46x 2 = 12, then 1+23
37no change7
28x 2 = 16, then 1+67
19no change9

Step 4 — Add Everything Up

8 + 5 + 3 + 7 + 7 + 9 + 0 + 1 + 4 + 3 + 8 + 5 + 3 + 7 + 7 + 9 = 86

Step 5 — Check if the Total Ends in Zero

Is 86 divisible by 10? No. It leaves a remainder of 6.

This card number is invalid.

That is the algorithm doing exactly what it was designed to do. The number 4567 8901 2345 6789 fails the Luhn check. Any payment form that runs this check would reject it instantly — before contacting your bank, before any network call, before anything.

For this number to be valid, the last digit would need to be 3 instead of 9. The card 4567 8901 2345 6783 gives a total of 80 — divisible by 10, valid.

Luhn Algorithm flowchart — enter card number, multiply odd positions by 1 and even positions by 2, sum all values, check mod 10, return valid or invalid


Valid vs Invalid — Side by Side

Card NumberLuhn TotalResult
Valid4567 8901 2345 678380Passes — divisible by 10
Invalid4567 8901 2345 678986Fails — remainder of 6

Where Is This Used in Real Life?

More places than you would think.

Credit and Debit Cards

Every card issued by every bank in every country — Visa, Mastercard, Amex, Maestro — uses the Luhn Algorithm. In Bangladesh, every Visa or Mastercard issued by Dutch-Bangla Bank, BRAC Bank, or any other local bank carries a valid Luhn check digit. Same goes for every card in the USA, Germany, Japan, and Canada.

Canada — Social Insurance Number

Canada’s SIN is a 9-digit national ID number. It officially uses the Luhn Algorithm as its validation method. Every SIN issued to every Canadian is a valid Luhn number.

USA — Card Networks

Every credit card transaction in America — processed by Chase, Bank of America, Citi, or any other bank — runs Luhn validation at every point of sale. Physical card readers and online checkouts both run it.

Germany — European Payments

Germany and the rest of the SEPA payment zone validate card numbers using Luhn as the first check before any transaction is processed.

Japan — JCB Cards

JCB is Japan’s own card network, used across Asia. Every JCB card number is a valid Luhn number, same as Visa or Mastercard.

IMEI Numbers — Every Phone, Every Country

Every phone has a 15-digit IMEI number. Dial *#06# on any phone to see it. The last digit is a Luhn check digit. Every phone sold anywhere in the world — including every phone sold in Bangladesh — carries a Luhn-validated IMEI.


What Luhn Does NOT Do

This is important. A lot of people misunderstand this.

What people assumeWhat is actually true
It confirms the card existsNo — it only checks the number format
It verifies the CVVNo — CVV is checked by your bank’s server
It checks if the card is expiredNo — expiry is a separate check
It confirms you have moneyNo — only your bank knows that
It catches stolen card numbersNo — it only catches typos

The Luhn Algorithm answers exactly one question: does this number follow the mathematical rule that all valid card numbers must follow? That is it. Nothing else.

It is the first filter. If a number fails Luhn, the system stops right there — no point going further. If it passes Luhn, the system moves on to the real checks at your bank.


Common Mistakes People Make When Typing Card Numbers

Even with Luhn running, people still get it wrong in predictable ways.

Swapping two adjacent digits — typing 6789 as 6798. Luhn is specifically designed to catch this. Every swap of two neighboring digits will fail the check.

Doubling a digit — typing 4445 as 4444. Luhn catches this too.

Missing a digit — entering 15 digits instead of 16. Systems check the length before running Luhn, so this gets caught even earlier.

Adding an extra digit — 17 digits instead of 16. Same as above.

Misreading worn digits — 1 and 7 look similar on a faded card. So do 6 and 8. Always check in good light.

Typing O instead of 0 — the letter O versus the digit zero. Common on old paper forms or when reading a card number out loud and typing it somewhere else.

The algorithm is mathematically guaranteed to catch every single-digit error and every transposition of two adjacent digits. Those two categories cover the vast majority of human typing mistakes. Luhn designed it that way deliberately.


Try It Yourself

You do not need a calculator or an app. Here is the method:

  1. Write out any 16-digit number
  2. Starting from the second-to-last digit, going left, underline every other digit
  3. Double each underlined digit. If the result is 10 or more, add its two digits
  4. Add all digits together — the doubled results plus the untouched ones
  5. If the total ends in 0 — the number is valid. Anything else — invalid

Practice number: 4539 1488 0343 6467

This is a standard test number that developers use. Walk through the steps and see if you get a total ending in 0. You should.


Interactive Widget Idea

If you wanted to build something around this — imagine a small tool where you type any card number, click a button, and watch each digit highlight as the algorithm processes it. Doubled digits in one color, untouched digits in another. The running sum builds up on screen. You get a final valid or invalid result with the total shown.

That is about 40 lines of JavaScript. Runs entirely in the browser, no server needed.


Conclusion

The Luhn Algorithm is one of the cleanest examples of a simple idea doing a big job. A few lines of arithmetic, running silently on every device that processes card numbers, protecting billions of transactions every day.

The next time a website catches your card typo instantly, you know what happened. A formula from 1960 ran in milliseconds and said no.

Hans Peter Luhn filed the patent on January 6, 1960. He called it “Computer for Verifying Numbers.” It has not needed a single update in 65 years.