Skip to content

Instantly share code, notes, and snippets.

@Adeniyii
Created August 2, 2020 09:17
Show Gist options
  • Save Adeniyii/668fa47cedbcbfbaecb93b7686e0c4b5 to your computer and use it in GitHub Desktop.
Save Adeniyii/668fa47cedbcbfbaecb93b7686e0c4b5 to your computer and use it in GitHub Desktop.
Credit Card Check cs50
// Applying Luhn's algorithm to validate credit cards
// Include c libraries
#include <stdio.h>
#include <cs50.h>
#include <math.h>
// Initialize implementation details
void finalCheck(string type, int evenList[], int len, int odd);
void CheckSum(long n, string type);
void checkType(long n, int length);
void checkCardLength(long n);
// Main program
int main(void)
{
long input;
do
{
input = get_long("Number: ");
}
while (input < 0);
// Check card number length
checkCardLength(input);
}
// Implementation detail to check card number length
void checkCardLength(long n)
{
// Get card number length
int length = log10(n) + 1;
switch (length)
{
case 16:
checkType(n, length);
break;
case 15:
checkType(n, length);
break;
case 13:
checkType(n, length);
break;
default:
printf("INVALID\n");
}
}
// Check for card type using the card number length and first number (or two) on card
void checkType(long n, int length)
{
if (length == 13)
{
int visa = n / pow(10, length - 1);
(visa == 4) ? CheckSum(n, "VISA") : printf("INVALID\n");
}
else if (length == 16)
{
int visa = n / pow(10, length - 1);
int v = n / pow(10, length - 1);
int mast = n / pow(10, (length - 2));
if (visa != 4)
{
(mast == 51 || mast == 52 || mast == 53 || mast == 54 || mast == 55) ? CheckSum(n, "MASTERCARD") : printf("INVALID\n");
}
else
{
CheckSum(n, "VISA");
}
}
else
{
int amex = n / pow(10, length - 2);
(amex == 34 || amex == 37) ? CheckSum(n, "AMEX") : printf("INVALID\n");
}
}
// Apply Luhn's algorithm for further checks
void CheckSum(long n, string type)
{
long newValue = 0;
int length = log10(n) + 1, evenList[length / 2], odd = 0;
for (int i = 0, j = (length / 2) + 1; j >= 1; i++, j--)
{
if (j == 1)
{
if (length == 16)
{
finalCheck(type, evenList, length, odd);
}
else
{
// add last num to odd
odd += n % 10;
finalCheck(type, evenList, length, odd);
}
}
else
{
// Add last num to odd
odd += n % 10;
// Remove last num
newValue = n / 10;
// Push product to evenList array
evenList[i] = (newValue % 10) * 2;
// Remove the last two card numbers
n /= 100;
}
}
}
// Add odd nums and destructured even nums, then return final result.
void finalCheck(string type, int array[], int len, int odd)
{
int sum = 0;
for (int i = 0; i < (int) len / 2; i++)
{
if (array[i] > 9)
{
// Destructure double digits
for (int j = 0; j < 2; j++)
{
sum += array[i] % 10;
array[i] /= 10;
}
}
else
{
sum += array[i] % 10;
}
}
// Add sum of odd nums with sum of even nums
int finalSum = sum + odd;
if (finalSum % 10 == 0)
{
printf("%s\n", type);
}
else
{
printf("INVALID\n");
}
}
// VICTORY!!!
@tzmueller
Copy link

Thanks for sharing..good to see how someone else approached this problem

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