Skip to content

Instantly share code, notes, and snippets.

@toshvelaga
Created July 25, 2018 23:17
Show Gist options
  • Save toshvelaga/4e09562fd65b9b7ad29a412c64dee4db to your computer and use it in GitHub Desktop.
Save toshvelaga/4e09562fd65b9b7ad29a412c64dee4db to your computer and use it in GitHub Desktop.
CS50 Credit
/* CS50 solution. Feel free to check with any online compiler. To get credit from Harvard you may have to make a few changes.
hmu at s.velaga@uky.edu if you have any questions. Also I haven't learned about proper code style and formatting -
if you wanna hmu to tell me this looks like shit and how to make it look better pls do */
#include <stdio.h>
#include <inttypes.h>
int main ()
{
printf ("Please enter a valid credit card number (without spaces) to calculate credit:\n");
unsigned long long c;
scanf ("%llu", &c);
printf ("You entered: %llu", c);
/* defines the intengers we are gonna use.
dig = "digit", eod = "every other digit" */
int dig, eod, eosum, sum, prod, sumprod, checksum, count;
sum = 0;
sumprod = 0;
eosum = 0;
count = 0;
/* sld is the second to last digit in the credit card # */
unsigned long long sld;
sld = c / 10;
/* PART A: counts how many digits are in the credit card # */
unsigned long long ccount;
ccount = sld * 10;
while (ccount > 0)
{
count++;
ccount = ccount / 10;
}
printf ("\nThe count is %d", count);
/* PART B: gets the first two digits and the first digit that are in the credit card # */
unsigned long long ccnum;
ccnum = sld * 10;
int firsttwodigits;
int firstdigit;
while (ccnum > 100)
{
ccnum = ccnum / 10;
firsttwodigits = ccnum % 100;
}
firstdigit = firsttwodigits / 10 % 10;
printf ("\nfirst two digits are %d", firsttwodigits);
printf ("\nfirst digit is %d", firstdigit);
/* PART C : this part adds up every other digit that was NOT multiplied by 2 */
while (c > 0)
{
eod = c % 10;
eosum += eod;
c = c / 100;
}
/* PART D : this part multiplies every other digit by 2 and adds them up */
while (sld > 0)
{
dig = sld % 10;
prod = dig * 2;
while (prod > 9)
{
prod -= 9;
}
sumprod += prod;
sld = sld / 100;
}
/* PART E : this part adds the sums found in PART C and PART D together */
checksum = eosum + sumprod;
printf ("\n\nSum of every other digit %d", eosum);
printf ("\nSum of every other digit multiplied by 2 is %d", sumprod);
printf ("\n\nChecksum is %d", checksum);
/* PART F: If the checksum ends with a 0 and the credit card # has either
13, 15, or 16 total numbers then it is valid */
if (checksum % 10 == 0 && (count == 13 || count == 15 || count == 16))
{
printf ("\nThe credit card number you entered is correct");
if (count == 15 && (firsttwodigits == 34 || firsttwodigits == 37))
{
printf ("\nYour credit card is AMEX");
}
else if (count == 16 && (firsttwodigits >= 51 && firsttwodigits <= 55))
{
printf ("\nYour credit card is MasterCard");
}
else if (count == 13 || count == 16 && (firstdigit == 4))
{
printf ("Your credit card is VISA");
}
}
else
{
printf ("\nI am sorry. The number you entered in is invalid");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment