Zamyla Chan: Let's check your credit. In this problem, we'll prompt the user for an input of a credit card number. Then we'll run that credit card number to see if it's valid. If so, we'll print the company that that credit card belongs to. Otherwise, we'll tell the user that that card is invalid.
Let's jump right in with prompting for user input. The credit card number is of data type long_long, so the CS50 library function get_long_long will be quite useful. But this function ensures that the user inputs any integer. So any positive integers, negative integers, or zero is all fine. So up to you to further validate whether or not the user has given us a valid credit card type number.
Now that we have a credit card number from the user, then we need to calculate the checksum. So credit card checksums are calculated as follows. Starting from the second to last digit, we multiply every other digit by 2. Then we add those products' digits together. From that we add the sum of the digits that weren't multiplied by 2 to that previous sum that we calculated. Finally, if that number ends in 0, then the number is valid.
Let's go through an example and take this step by step. So our first step is to start from the second to last digit and multiply every other digit by two. Now, if I stored the credit card number in a variable called CC number, then modding that by 10 would give me the very last digit. So how might you access the second to last digit?
OK, so once we've accessed the second to last digit, then we'll iterate through multiplying every other digit by 2. Once we have that, then we'll take those products and add those products' digits together. So here I have single digits, so that's fine. But then once I get to my last number-- 7 multiplied by 2-- I add the 1 and the 4 to give me 27. From there, we add that product-- 27-- to the sum of the digits that weren't multiplied by 2.
So here I've highlighted all of those numbers in orange. Once we add those digits, then we get our final checksum number. So we validate it by making sure that the last digit is 0. If the checksum does not end in 0, then the credit card number is definitely invalid. Otherwise, let's go on to check the company identifiers and the credit card number length.
We've provided a list of three companies, along with that, the number of integers that their credit card numbers have. Then the first two digits that those cards might start with. So up to you to keep track of the first two digits of the credit card and the number of integers in that card. And with that, you've finished the problem. My name is Zamyla, and this was Credit.