The Luhn algorithm, also known as the modulus 10 or mod 10 algorithm, is a simple checksum formula used to validate various identification numbers, most notably credit card numbers. This project implements the Luhn algorithm to validate credit card numbers and provides a detailed explanation of how the algorithm works.
The Luhn algorithm checks whether a given credit card number is valid by performing the following steps:
-
Reverse the Credit Card Number:
- Start with the rightmost digit (check digit) and move left.
-
Double Every Second Digit:
- Beginning with the first digit on the right, double the value of every second digit.
- If doubling a digit results in a two-digit number (e.g., 7 ร 2 = 14), add the digits of the product together (e.g., 1 + 4 = 5).
-
Sum All Digits:
- Add all the single digits obtained from the above step to the digits that were not doubled.
-
Check the Total:
- If the total modulo 10 equals 0, the credit card number is valid; otherwise, it is invalid.
For the credit card number 4539 1488 0343 6467:
- Reverse:
7 6 4 6 3 4 3 0 8 8 4 9 3 5 4. - Double every second digit: 7, (6ร2=12), 4, (6ร2=12), 3, (4ร2=8), 3, (0ร2=0), 8, (8ร2=16), 4, (9ร2=18), 3, (5ร2=10), 4
- Replace double-digit results with their digit sums: 7, 3, 4, 3, 3, 8, 3, 0, 8, 7, 4, 9, 3, 1, 4
- Sum all digits: 7 + 3 + 4 + 3 + 3 + 8 + 3 + 0 + 8 + 7 + 4 + 9 + 3 + 1 + 4 = 69
- Check total:
69 % 10 = 9(Invalid card number).