Skip to content

Instantly share code, notes, and snippets.

@aryan348
Created July 18, 2020 19:41
Show Gist options
  • Save aryan348/d390ec7aa4441ee086846516267cde4d to your computer and use it in GitHub Desktop.
Save aryan348/d390ec7aa4441ee086846516267cde4d to your computer and use it in GitHub Desktop.
CS50 Problem Set 1 (Fall 2020) - Credit
#include <math.h>
#include <cs50.h>
#include <stdio.h>
// Function Declaration
int creditCardType(long cardNumber);
int main(void)
{
long c;
do
{
// Get Credit Card number
c = get_long("Number: ");
}
// do while to not accept less than 12 digit numbers
while (c < 12);
// calculate length of entered card
int length = 0;
long calcuLength = c;
while (calcuLength != 0)
{
calcuLength /= 10;
length++;
}
long myNum = c;
int digit;
int position;
int multiply = 0;
int sum = 0;
int output;
// checksum if the card is valid
for (position = 1; position <= length; position ++)
{
// get last credit card number
digit = myNum % 10;
// reverses the credit card number for running Luhn’s Algorithm
myNum = myNum / 10;
// First Step of Luhn's Algorithm
// Multiply every other digit by 2, starting with the number’s second-to-last digit,
// and then add those products’ digits together.
// Below it gets every other digit
if (position % 2 == 0)
{
// Multiplying digit by 2
multiply = digit * 2;
// adding those digit together
sum += multiply % 10;
sum += multiply / 10;
}
// According to Luhn's Algorithm than:
// Add the sum to the sum of the digits that weren’t multiplied by 2
// This gets digits which were not multiplied by 2
if (position % 2 != 0)
{
// Adding the sum of digit
sum = sum + 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!
if (sum % 10 == 0)
{
// function runs to tell which card company
output = creditCardType(c);
return 0;
}
else
{
printf("INVALID\n");
return 0;
}
}
// function for card company
int creditCardType(long cardNumber)
{
// mastercard first two digit is 34 or 37
// dividing by 10000000000000 gets first two digit
if (cardNumber / 10000000000000 == 34 || cardNumber / 10000000000000 == 37)
{
printf("AMEX\n");
return 0;
}
// Mastercard uses 16 digit numbers and start with 51....55
else if (cardNumber / 100000000000000 >= 51 && cardNumber / 100000000000000 <= 55)
{
printf("MASTERCARD\n");
return 0;
}
// visa uses 13 & 16 and starts with 4
else if (cardNumber / 1000000000000000 == 4 || cardNumber / 1000000000000 == 4)
{
printf("VISA\n");
return 0;
}
else
{
printf("INVALID\n");
return 0;
}
printf("\n");
return 0;
}
@BaconSalamiTurkey
Copy link

hi, why would c < 12 mean c cannot be fewer than 12 digits? wouldn't it mean c must be more than number 12?

@aryan348
Copy link
Author

Because credit card minimum number starts with 13(VISA) so it should not accept fewer than 12 digits. I could also write c < 13 to be more precise.

@adeniyii
Copy link

adeniyii commented Aug 2, 2020

On line 19, "while c < 12" literally means it should accept any values from 13 upward rather than values of length 13. You should first get the length of c before using that condition.

@tzmueller
Copy link

Clever solution and easy to follow your documentation..thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment