Instantly share code, notes, and snippets.

# CraigRodrigues/credit.c Created Jun 3, 2016

My solution to CS50 Hacker pset1 - "Bad Credit"
 #include #include #include #include #include // 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) { // get card number from user printf("Number: "); long long card_num = GetLongLong(); // use digits function int card_digits = getCardDigits(card_num); // if not a valid credit card length print invalid immediately if (card_digits != 13 && card_digits != 15 && card_digits != 16) { printf("INVALID\n"); return 0; } // 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; count = count/10; i++; } // 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++; } 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]; } } // 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++; } int other_digit_count = 0; for (int j = 0; j < (card_digits - (card_digits/2)); j++) { other_digit_count = other_digit_count + numberArray3[j]; } int companyId; int companyId2; // validating Luhn algorithm if ((digit_times_two + other_digit_count) % 10 == 0) { companyId = numberArray[0]; companyId2 = numberArray[1]; } else { printf("INVALID\n"); return 0; } // 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; }
Owner Author

### CraigRodrigues commented Jun 3, 2016 • edited

 STEPS: Prompt user for credit card number. (long long) Check length of the card #. If not valid print INVALID. Function: cardLength to get the number of digits in a card number using Log10 + 1. Put the digits into an array by using % and a loop and /10 etc. Loop from the back to the front of the new array of digits and take every other digit. Multiply the results of 5 by 2. Add the individual digits together (what do you do about double digits?) Add to #7 the sum of the remaining digits. If answer to #8's final digit is a zero then the card number is legit. Check the card's first digit(s) to get the card company. If #9 & #10 are true then print out what card company it is and return 0. NOTES/THOUGHTS: Had to do a bit of research for the questions I had such as how to find out the number of digits in an integer and how to put the individual digits of a number into an array. Once I found those it was just a matter of piecing the puzzle together doing down the list of steps above. One loop I had initialized the counter i inside the loop itself so it actually never incremented. That took a while to see, yet it was such a simple problem! I had to figure out how to reverse a loop to put the digits into it back to front instead of front to back. Put "return 1" in a couple of places when the program really wanted "return 0" regardless. Need to remember that using the data type int only takes the integer portion not anything after the decimal. Useful to remember for this problem set.

### CaptainDaVinci commented Sep 14, 2016

 Have you encountered segmentation fault while compiling?

### champi-dev commented Feb 4, 2017

 This seems to be a little complicated for me. Week 1 on Cs50x, pset 1 I've already done: hello.c, water.c, mario.c, greedy.c But I'm still trying to understand how to proceed with credit.c I've read your code and It helped to understand a few things but I'm getting lost from line 31.

### shedevralen commented Jul 21, 2018

 I don't understand since 38th line.