Card numbers (13 to 16 digits) carry a built-in check, the Luhn algorithm:
- From the right, double every second digit; if the result has two digits, add them (16 → 1 + 6 = 7).
- Add all the results of step 1.
- Add all the other digits (first, third… from the right).
- The number is valid if the total of steps 2 and 3 is divisible by 10.
The first digits give the brand: 4 Visa, 5 Mastercard, 37 American Express, 6 Discover. Write CardCheck with the methods isValid, sumOfDoubledDigits, digitValue, sumOfOtherDigits, countDigits, prefix(number, k) and brand. The number is a long. main reads numbers until the end of the input. For the input
4388576018410707 4388576018402626 5105105105105100 371449635398431 6011111111111117 4111111111111112 123456
the program prints
4388576018410707: sum 70, valid, Visa
4388576018402626: sum 75, invalid
5105105105105100: sum 20, valid, Mastercard
371449635398431: sum 80, valid, American Express
6011111111111117: sum 30, valid, Discover
4111111111111112: sum 31, invalid
123456: sum 21, invalid
(The numbers used here are published test numbers, not real cards.)