# The Luhn Algorithm - XML

The Luhn formula, created in the late 1960s, is a standard algorithm used to validate the details of a credit card number. It is popularly called the mod 10 or modulus 10 algorithm. To validate a credit card number, the Luhn algorithm uses a check digit, which is the last digit in the credit card number. The check digit is generated by applying the Luhn formula to the credit card number. Therefore, to validate the credit card number, the check digit is passed to the Luhn algorithm.

To help you understand the Luhn formula, the following list discusses how it works:

1. Moving left and starting from the second to the last digit, multiply the values of all alternate digits by 2. If the resulting digits are two-digit numbers, add the two resulting digits to form a one-digit number.
2. Starting from the left, add the digits that are not multiplied by 2 in step 1 to the results of the individual digits of step 1.
3. Multiply the result by 10.
4. The result of step 2 should end with a 0. The result must be 0 for the credit card number to be valid.

Note
It is important to note that the Luhn algorithm does not actually validate a credit card number. Instead, it just validates that the number, entered by a user, conforms to the algorithm. The actual credit card validation can happen only through the bank that issues the credit card.

You can use the Luhn algorithm to validate the credit card number prior to validating the number against the bank records. This ensures that the user does not enter junk data and avoids typos.

XML Topics