What is Luhn’s Algorithm ? How to use it to validate credit card number ?
0
/
1612
What is Luhn’s Algorithm?
Luhn Algorithm is also known as modulo 10 algorithm or modulus 10 formula. It’s a checksum which is used to validate credit card, IMEI and various national identification numbers from across the globe. It’s named after an IBM scientist Hans Peter Luhn.Overview
Luhn test is used by various merchants and credit card numbers to check if the provided credit card number is valid or not. The algorithm has 5 simple steps as listed below- Reverse the digits in the given credit card number string.
- Take first, third and every odd digit from the string and sum them up to get partial sum s1.
- Take second, fourth and every other even digit and list them together.
- Multiply each of the digits listed in step 3 by 2 and sum the digits if the result is greater than 9. Then add the final digits together to get second half of the partial sum s2.
- Sum s1 and s2 and take the modulo of the sum by 10. If the result is zero, given number passes the Luhn test, if not, it fails and is an invalid number.