Luhn Algorithm Calculator
Table of contents
What is the Luhn algorithm?How does the Luhn algorithm work?Step by step on how to calculate the Luhn check digitUsing the Luhn algorithm calculator – credit card number checkerFAQsOur Luhn algorithm calculator can test a number with Luhn validation and find the check digit for a given number. This tool has different applications such as:
- Gift card number generator 🎁;
- Credit card number checker 💳; and
- Many more!
Keep reading to learn its use cases and find the answer to questions such as 'What is the Luhn algorithm?', 'What is Luhn validation?' or 'How do I calculate check digit with Luhn algorithm?'.
Let's dive right in!
What is the Luhn algorithm?
The Luhn algorithm or mod 10 is a method of validating numbers using simple operations on each digit. It can detect common typing errors, and because of that, companies use it as pre-validation with credit card numbers.
It was created by the German computer scientist Hans Peter Luhn.
The Luhn algorithm can only detect single-digit and almost all permutations errors. However, it cannot catch double errors such as 22↔55
, 33↔66
and 44↔77
.
Now, let's see how a simple algorithm can act as a credit card number checker.
How does the Luhn algorithm work?
The Luhn algorithm works by taking a number and doing some basic math operations to every digit except the last one. This last digit is called the check digit. According to the Luhn algorithm, if the result of the operations and the check digit are equal, the number is valid.
Credit card, IMEI, and gift card numbers are created so that they pass a Luhn digit check, i.e., after applying the operations to every digit, the result matches the last one.
Step by step on how to calculate the Luhn check digit
- To begin using the algorithm, we need a number, let's say
23459034
.- As we said, the right-most digit is the check digit (
2345903[4]
), so we need to separate the number and the check digit:2345903
and4
.
- As we said, the right-most digit is the check digit (
- Now, the algorithm operates from right to left, so starting from the last digit of the already cut number (in this case, the last number is 3). We multiply every other digit by two and leave the others intact:
2 | 3 | 4 | 5 | 9 | 0 | 3 |
---|---|---|---|---|---|---|
x2 | x2 | x2 | x2 | |||
4 | 3 | 8 | 5 | 18 | 0 | 6 |
- If the multiplied number is equal to or greater than
10
, we subtract9
from it.
2 | 3 | 4 | 5 | 9 | 0 | 3 |
---|---|---|---|---|---|---|
x2 | x2 | x2 | x2 | |||
4 | 3 | 8 | 5 | 18 | 0 | 6 |
4 | 3 | 8 | 5 | 18-9 | 0 | 6 |
4 | 3 | 8 | 5 | 9 | 0 | 6 |
- Next, we take the modulo 10 (check our modulo calculator if you don't know what this is) of the sum of all the final digits (
4 + 3 + 8 + 5 + 9 + 6 = 35
), the result is the number that should be our check digit to pass the Luhn validation:
- Finally, we compare the result with our check digit. If it matches, the number is valid. Otherwise, it is not. In this case, we can easily see that
5 ≠ 4
, and because of that:2345903[4]
will be an invalid number.2345903[5]
will be a valid number.
This is how a gift card number generator and credit card number checker works; by comparing the check digit with the operations on all other digits.
🙋 Keep in mind that this validation does not guarantee that there actually exists a credit card or gift card number linked with that number. It just means that if it does exist, it will pass a Luhn validation.
Using the Luhn algorithm calculator – credit card number checker
The Luhn algorithm calculator uses two methods based on the same algorithm:
- Calculate the Luhn check digit for a given number; and
- Test a number for Luhn validation.
Calculate the Luhn check digit for a given number
With this method, the Luhn algorithm calculator will basically work like a gift card number generator or Luhn algorithm generator.
Simply input any positive integer, and the calculator will output the digit you should append to it, so it passes a Luhn validation. You can verify that your number is valid using the other method.
Test a number for Luhn validation
This method simply runs the Luhn algorithm in the input number and compares the result with its last digit. A message will automatically appear stating that the number is valid or showing the correct check digit to pass the validation if it's invalid.
What is Luhn validation?
Luhn validation is the process of comparing a number with its last digit (or check digit) using the Luhn algorithm to test whether a given number is valid. Credit card companies widely use it before testing if the credit card is linked to an account.
What is the check digit for 5435392 using Luhn algorithm?
2. The check digit is the right-most digit. This number is compared with the result of running the Luhn algorithm through all other digits. If the result and the check digit are equal, the number is valid.
How do I generate a gift card number?
To generate a gift card number:
- Choose the final length of the number and subtract one from it.
- Pick a random number that matches the length of the previous step (after subtracting one).
- Multiply by two every other digit starting from the last one.
- If any of the new numbers is greater than or equal to 10, subtract 9 from it.
- Add all the resulting digits and write down this number.
- Take the mod 10 of this final number.
- Append this digit to your initial number. This number will pass a Luhn validation and be a valid gift card number. Congrats!
Are credit card numbers random?
No, credit card numbers are created to pass a pre-validation known as Luhn validation. After this initial validation, each company has different methods to assign numbers to a client's ID, account number, etc.