A payment card number is 16 digits long. The leftmost 6 digits identify the bank that issued the card. The next 2 digits give the card type, for example debit, credit, or gift. Digits 9 to 15 are the serial number of the card, and the last digit is a control digit that says whether the whole number is valid. A payment program can therefore catch most mistyped numbers on the spot.
In a valid card number the last digit is chosen so that this procedure succeeds:
- Label the digits from left to right with 1 to 16.
- Multiply every odd-labeled digit by 2.
- If a result of step 2 is greater than 9, subtract 9 from it.
- Add up the results of step 3, then add the sum of all even-labeled digits.
- If the total is a multiple of 10, the card number is valid. Otherwise it is invalid.
Read several card numbers and decide for each one whether it is valid.