
e) Add together all the results and add the checking digit. If any of the results are greater than 9, subtract 9 from those numbers. c) For this sequence of reversed digits, take the digits at each of the even indices (0, 2, 4, 6, d) etc.) and double them. b) Reverse the order of the remaining digits. This number is called the checking digit, and it will be excluded from most of our calculations. The way we're going to use the algorithm is as follows: a) Remove the rightmost digit from the card number. The purpose of the algorithm is to identify potentially mistyped numbers, because it can determine whether or not it's possible for a given number to be the number for a valid card.
ALGORTHM FOR A CREDIT CARD VALIDATOR SERIAL
This algorithm is used in real-life applications to test credit or debit card numbers as well as SIM card serial numbers. The algorithm we're going to use to verify card numbers is called the Luhn algorithm, or Luhn formula.

When the program is complete, we're going to be able to determine whether a given card number is valid or not. (Why?)Ĭonsider the below representative of how your own program should behave when passed a valid credit card number (sans hyphens).Transcribed image text: Program Requirements: For this lab, were going to be writing a simple credit card validator. But do not assume that the user’s input will fit in an int! Best to use get_long from CS50’s library to get users’ input. For simplicity, you may assume that the user’s input will be entirely numeric (i.e., devoid of hyphens, as might be printed on an actual card). So that we can automate some tests of your code, we ask that your program’s last line of output be AMEX\n or MASTERCARD\n or VISA\n or INVALID\n, nothing more, nothing less. In a file called credit.c in a ~/pset1/credit/ directory, write a program that prompts the user for a credit card number and then reports (via printf) whether it is a valid American Express, MasterCard, or Visa card number, per the definitions of each’s format herein. So, validating credit card numbers isn’t hard, but it does get a bit tedious by hand. Yup, the last digit in that sum (20) is a 0, so David’s card is legit! Now let’s add that sum (13) to the sum of the digits that weren’t multiplied by 2 (starting from the end): Now let’s add those products’ digits (i.e., not the products themselves) together: Okay, let’s multiply each of the underlined digits by 2:ġ That’s kind of confusing, so let’s try an example with David’s Visa: 4003600000000014.įor the sake of discussion, let’s first underline every other digit, starting with the number’s second-to-last digit: If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!.Add the sum to the sum of the digits that weren’t multiplied by 2.Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.According to Luhn’s algorithm, you can determine if a credit card number is (syntactically) valid as follows: So what’s the secret formula? Well, most cards use an algorithm invented by Hans Peter Luhn of IBM. Of course, a dishonest mathematician could certainly craft a fake number that nonetheless respects the mathematical constraint, so a database lookup is still necessary for more rigorous checks. That checksum enables computers (or humans who like math) to detect typos (e.g., transpositions), if not fraudulent numbers, without having to query a database, which can be slow. But credit card numbers also have a “checksum” built into them, a mathematical relationship between at least one number and others. All American Express numbers start with 34 or 37 most MasterCard numbers start with 51, 52, 53, 54, or 55 (they also have some other potential starting numbers which we won’t concern ourselves with for this problem) and all Visa numbers start with 4. And those are decimal numbers (0 through 9), not binary, which means, for instance, that American Express could print as many as 10^15 = 1,000,000,000,000,000 unique cards! (That’s, um, a quadrillion.)Īctually, that’s a bit of an exaggeration, because credit card numbers actually have some structure to them.

There are a lot of people with credit cards in this world, so those numbers are pretty long: American Express uses 15-digit numbers, MasterCard uses 16-digit numbers, and Visa uses 13- and 16-digit numbers. Printed on that card is a number that’s also stored in a database somewhere, so that when your card is used to buy something, the creditor knows whom to bill. A credit (or debit) card, of course, is a plastic card with which you can pay for goods and services.
