Skip to content

Instantly share code, notes, and snippets.

@qbatten
Last active June 10, 2018 18:47
Show Gist options
  • Save qbatten/9f3c62fe94aadbdf8367247390b180c0 to your computer and use it in GitHub Desktop.
Save qbatten/9f3c62fe94aadbdf8367247390b180c0 to your computer and use it in GitHub Desktop.
/*
This program starts by asking the user to input a credit card number. Then it uses Luhn's algorithm to test
the validity of the card number, uses some simple tests to figure out what kind of credit
card it is (Visa, Amex, or Mastercard), and prints the results.
This was my submission (submitted on June 7th) for a problem set from the CS50 class on edX.
Here's a link to the text of the assignment:
https://docs.cs50.net/2018/x/psets/1/credit/credit.html
*/
#include <stdio.h>
#include <cs50.h>
int main(void)
{
long long num = get_long_long("Number: "); //Get number from user.
int numLength = 0; //The number of digits in the credit card number.
int cardType = 0; //1 = Amex, 2 = Master, 3 = Visa
int valid = 0; // Is it valid?
//we increment valid up by 1 when it passes a check.
//there are 3 validity checks (length, first digits, Luhn's algorithm)
//so if valid = 3, the number is good.
//CHECK 1: Length
if (num > 999999999999 && num < 10000000000001) // 13 digit
{
numLength = 13;
valid = valid + 1;
}
else if (num > 99999999999999 && num < 1000000000000001) // 15 digit
{
numLength = 15;
valid = valid + 1;
}
else if (num > 999999999999999 && num < 10000000000000001) // 16 digit
{
numLength = 16;
valid = valid + 1;
}
else
{
printf("INVALID\n");
exit(0);
}
// Load the number into an array of digits.
// We want the digits to go forward, so this loop needs to go backwards.
//1. I know there's a better way to do this than using numLength to tell the program when to stop reading from the array,
// but you cant initialize an array using a variable and we haven't dug into memory stuff yet.
//2. I start it at 1 (which isn't standard, right?) because I do a bunch of loops that count down and the alternative
// would be to make numLength smaller than it really is, which seems weird too. Not sure what the right answer is here.
int digits[16] = {0};
long long numConv = num;
for (int i = numLength; i > 0; i--)
{
digits[i] = numConv % 10;
numConv = (numConv - numConv % 10) / 10;
}
//CHECK 2: initial digits & assign cardtype
if (numLength == 15 && digits[1] == 3 && (digits[2] == 4 || digits[2] == 7)) //
{
//Amex numbers are 15 digits long and start w 24 or 27
cardType = 1;
}
else if (numLength == 16 && digits[1] == 5 && digits[2] > 0 && digits[2] < 6)
{
//Mastercard numbers are 16 digits and start w 51, 52, 53, 54, or 55
cardType = 2;
}
else if ((numLength == 13 || numLength == 16) && digits[1] == 4)
{
//Visa numbers are 13 or 16 digits and start w 4
cardType = 3;
}
else
{
//If it doesn't fit those criteria, it's not valid!
printf("INVALID\n");
exit(0);
}
//CHECK 3: Luhn's Algorithm
int start = numLength - 1; //Start at the second-to-last digit.
int tempDigit = 0; //The sum we're getting to...will be the final result of the algorithm.
for (int i = start; i > 0; i = i - 2) //Run through every other digit starting w second-last digit.
{
digits[i] = digits[i] * 2; //Multiply each digit by 2.
if (digits[i] > 9) //If the result is a 2-digit number, add the digits. (AKA add 1, because you'll never get a higher result than 18).
{
tempDigit = tempDigit + (digits[i] % 10) + 1;
}
else //If its a one-digit number, add that digit to tempDigit.
{
tempDigit = tempDigit + digits[i];
}
}
for (int i = numLength; i > 0; i = i - 2) //Now do the other half of the digits, just add them each onto tempDigit.
{
tempDigit = tempDigit + digits[i];
}
if (tempDigit % 10 == 0)
{
valid = valid + 1;
}
else
{
printf("INVALID\n");
}
//Print card type... if we're still here, it's a valid number, and cardType has been assigned an accurate value.
switch (cardType)
{
case 1 :
printf("AMEX\n");
break;
case 2 :
printf("MASTERCARD\n");
break;
case 3 :
printf("VISA\n");
break;
default :
printf("ERROR\n"); //Just in case.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment