Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My solution to CS50 Hacker pset1 - "Bad Credit " (printf tests included)
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
// calculates the number of digits in the card number
int getCardDigits(long long card_num)
{
int card_digits = (int)log10(card_num) + 1;
return card_digits;
}
int main(void)
{
long long card_num = GetLongLong();
printf("CC #: %lld\n", card_num);
int card_digits = getCardDigits(card_num);
printf("Digits: %i\n", card_digits);
// if not a valid credit card length print invalid immediately
if (card_digits != 13 && card_digits != 15 && card_digits != 16)
{
printf("INVALID\n");
return 1;
}
// storing the individual digits in an array
int numberArray[card_digits];
long long count = card_num;
int i = 0;
while (count != 0)
{
numberArray[card_digits - 1 - i] = count % 10;
printf("Array %i is %i\n", card_digits - 1 - i, numberArray[card_digits - 1 - i]);
count = count/10;
printf("New card number is %lli\n", count);
i++;
}
for (int j = 0; j < card_digits; j++)
{
printf("%i", numberArray[j]);
}
printf("\n");
// Every other number starting from the second to last in array
int numberArray2[card_digits/2];
int k = 2;
int l = 0;
while ((card_digits - k) > -1)
{
numberArray2[l] = 2 * numberArray[card_digits - k];
k = k + 2;
l++;
}
for (int j = 0; j < card_digits/2; j++)
{
printf("%i\n", numberArray2[j]);
}
int digit_times_two = 0;
for (int j = 0; j < card_digits/2; j++)
{
if (numberArray2[j] > 9)
{
int split_digit = 0;
digit_times_two = digit_times_two + (numberArray2[j] % 10);
split_digit = numberArray2[j]/10;
digit_times_two = digit_times_two + split_digit;
}
else
{
digit_times_two = digit_times_two + numberArray2[j];
}
printf("Sum of 2x digits = %i\n", digit_times_two);
}
// the remainging digits added together after put in 3rd array
int numberArray3[card_digits - (card_digits/2)];
int m = 1;
int n = 0;
while ((card_digits - m) > -1)
{
numberArray3[n] = numberArray[card_digits - m];
m = m + 2;
n++;
}
for (int j = 0; j < (card_digits - (card_digits/2)); j++)
{
printf("%i\n", numberArray3[j]);
}
int other_digit_count = 0;
for (int j = 0; j < (card_digits - (card_digits/2)); j++)
{
other_digit_count = other_digit_count + numberArray3[j];
printf("Other digit sum = %i\n", other_digit_count);
}
int companyId;
int companyId2;
// validating Luhn algorithm
if ((digit_times_two + other_digit_count) % 10 == 0)
{
companyId = numberArray[0];
companyId2 = numberArray[1];
printf("VALID\n");
}
else
{
printf("INVALID\n");
return 1;
}
// checking which card company
if (card_digits == 15 && companyId == 3 && (companyId2 == 4 || companyId2 == 7))
{
printf("AMEX\n");
}
else if (card_digits == 16 && companyId == 5 && (companyId2 >= 1 && companyId2 <= 5))
{
printf("MASTERCARD\n");
}
else if ((card_digits == 13 || card_digits == 16) && companyId == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
return 0;
}
@CraigRodrigues

This comment has been minimized.

Copy link
Owner Author

CraigRodrigues commented Jun 3, 2016

Here is my code including all the printf functions and loops I used to test my program as I went. Not sure of a better way to make sure things are working as I went through the steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.