Skip to content

Instantly share code, notes, and snippets.

@ThatOneCubanDude
Created March 26, 2020 22:53
Show Gist options
  • Save ThatOneCubanDude/711c0d07ef2ade5b046cd8cdeda9e7a1 to your computer and use it in GitHub Desktop.
Save ThatOneCubanDude/711c0d07ef2ade5b046cd8cdeda9e7a1 to your computer and use it in GitHub Desktop.
My solution to CS50x Problem Set 1 - Credit. Feel free to critique or ask questions.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
//gets a valid number from the user
long long int credit_card;
do
{
credit_card = get_long_long("Number: ");
}
while(credit_card < 0);
//printf("%lld\n", credit_card);
int digits = 0;
long long int number = credit_card;
//printf("%lld", number);
for (digits = 0; number/10 > 0; digits++)
{
number = number/10;
}
digits++;
//printf("%d\n", digits);
// checks if card is a valid length
if (digits != 13 && digits != 15 && digits != 16)
{
printf("INVALID\n");
return 0;
}
//stores every of the credit card as a variable
int d1 = ((credit_card/(1)) % 10);
int d2 = ((credit_card/(10)) % 10);
int d3 = ((credit_card/(100)) % 10);
int d4 = ((credit_card/1000) % 10);
int d5 = ((credit_card/10000) % 10);
int d6 = ((credit_card/100000) % 10);
int d7 = ((credit_card/1000000) % 10);
int d8 = ((credit_card/10000000) % 10);
int d9 = ((credit_card/100000000) % 10);
int d10 = ((credit_card/1000000000) % 10);
int d11 = ((credit_card/10000000000) % 10);
int d12 = ((credit_card/100000000000) % 10);
int d13 = ((credit_card/1000000000000) % 10);
int d14 = 0;
int d15 = 0;
int d16 = 0;
if (digits == 15)
{
d14 = ((credit_card/10000000000000) % 10);
d15 = ((credit_card/100000000000000) % 10);
}
if (digits == 16)
{
d14 = ((credit_card/10000000000000) % 10);
d15 = ((credit_card/100000000000000) % 10);
d16 = ((credit_card/1000000000000000) % 10);
}
//finds the digits of each second multiplied by 3, adds them together, and stores it
int sumd2 = ((d2*2) % 10)+(((d2*2)/10) % 10);
int sumd4 = ((d4*2) % 10)+(((d4*2)/10) % 10);
int sumd6 = ((d6*2) % 10)+(((d6*2)/10) % 10);
int sumd8 = ((d8*2) % 10)+(((d8*2)/10) % 10);
int sumd10 = ((d10*2) % 10)+(((d10*2)/10) % 10);
int sumd12 = ((d12*2) % 10)+(((d12*2)/10) % 10);
int sumd14 = ((d14*2) % 10)+(((d14*2)/10) % 10);
int sumd16 = ((d16*2) % 10)+(((d16*2)/10) % 10);
//adds together the digits of every second number
int sum1 = sumd2+sumd4+sumd6+sumd8+sumd10+sumd12+sumd14+sumd16;
//adds together the digits of the remaining numbers
int sum2 = d1+d3+d5+d7+d9+d11+d13+d15;
//adds those two together (why? because I didn't want to get confused)
int check = sum1+sum2;
//finds the last value of that overall sum
int checksum = (check % 10);
//checks to see if last value is 0
if (checksum != 0)
{
printf("INVALID\n");
return 0;
}
//printf("%d\n", checksum);
if (digits == 15)
{
if (d15 == 3)
{
if (d14 == 4 || d14 == 7)
{
printf("AMEX\n");
return 0;
}
else
{
printf("INVALID\n");
return 0;
}
}
else
{
printf("INVALID\n");
return 0;
}
}
else if (digits == 13)
{
if (d13 == 4)
{
printf("VISA\n");
return 0;
}
else
{
printf("INVALID\n");
return 0;
}
}
else if (digits == 16)
{
if (d16 == 4)
{
printf("VISA\n");
return 0;
}
else if (d16 == 5)
{
if (d15 == 1 || d15 == 3 || d15 == 3 || d15 == 4 || d15 == 5)
{
printf("MASTERCARD\n");
return 0;
}
else
{
printf("INVALID\n");
return 0;
}
}
else
{
printf("INVALID\n");
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment