BRIAN: In credit, your task is going to be to take a credit card number
and determine whether that number is an American Express credit card
number, a MasterCard number, a Visa number or maybe, something else
entirely.
How are you going to do this?
Well, credit card numbers follow particular patterns.
American Express card numbers, for example, are generally 15 digits,
and start with the digits 3, 4 or 3, 7.
MasterCard numbers will usually have 16 digits,
and often start with 51, 52, 53, 54, or 55.
Of course, there are other MasterCard numbers that don't necessarily
follow this pattern, but for the purposes of this problem,
we're just going to look at this subset of MasterCard numbers.
And finally, Visa numbers are 13 or 16 digits,
and will start with the digit 4.
But not every sequence of 13 digits that starts with the digit 4
is, necessarily, a Visa card number.
Every valid credit card number also satisfies a checksum,
a relationship between the digits of that credit card number
that determine whether that particular sequence of numbers
is a valid credit card number or not.
How does this checksum algorithm work?
Well, it works as follows.
We first, multiply every other digit of the credit card number by 2,
starting with the second to last digit.
Then we add those products' digits together to get a number,
and we add that number to the sum of all the digits
that we didn't multiply by 2.
If we take that final result, and the last digit is zero,
then the credit card number is valid.
And if it's not, then it's invalid.
Let's take a look at an example to see this algorithm in practice.
Here's an example of a credit card number.
The first thing we're going to do is take every other digit,
starting with the second to last digit.
So we're looking at the 1, the 0, the 0, the 0, the 0, the 6, the 0, and the 4.
And we're going to take each of those digits and multiply them by 2.
The next step is going to be to add all of these individual digits together.
So 8 plus 0 plus 1 plus 2, because those are the digits in 12,
plus 0 plus 0 plus 0 plus 0 plus 2, that's going to give us the sum 13.
We're going to take 13 and add that to all of the other digits
that we didn't multiply by 2.
So 13 plus 0 plus 3 plus 0 plus 0 plus 0 plus 0 plus 0 plus 4,
that result is going to give us the number 20.
This is our final result. The last digit is 0,
so this credit card number satisfies the checksum.
It could be a valid credit card number.
So one of your tasks in this problem is going to be,
given the credit card number, figure out if it satisfies the checksum or not.
And to do that, you're going to need to take a big number
and get at the individual digits of the number.
But how do we get the digits of the number?
Let's take a big number, like this credit card number,
and consider how would we get the last digit of this credit card number, which
in this case is the number 4?
Well, notice that if you take any big number, and take the remainder.
When you divide it by 10, what you get is the rightmost digit.
If we take this big number, divide it by 10, and take the remainder,
the remainder is 4, the last digit.
And in code, we have the modulo operator that gets us the remainder
when we divide it by something.
So if we take this big number, and take it modulo 10,
that will give us the remainder, when we take this number
and divide it by 10, which in this case is 4.
So this gives us a way of taking a big number
and figuring out what the last digit is.
And I'll leave it up to you to figure out
how to adapt this to get the second to last digit, or the third to last digit,
for example.
What do you need to do in this problem, then?
Well, you'll first want to prompt the user to type
in their input, a credit card number.
Then, you'll want to calculate the checksum to figure out
if it could be a valid credit card or not,
based on if that checksum has a final digit that is or isn't a 0.
After you do that, you'll want to check the credit card's length, as well
as the starting digits, to figure out if it matches the pattern for an American
Express card or a Visa or a MasterCard.
And if it matches the checksum and also matches
the card length and starting digit requirements,
then your program should print out Amex for an American Express
card, MasterCard for MasterCard, Visa for a Visa card,
and if it doesn't satisfy any of the three,
your program should print out invalid.
After you do that, you should be able to test your program
on some sample valid and invalid credit card numbers,
and make sure that you get the right result.
My name is Brian, and this was credit.