

Again, you don't have to think about this now, but in future you gonna get in trouble if you have to do your work with big data in nearly no time). The if statements inside the switch statement are very long and doing heavy stuff many times (pow and division are expensive operators. It is 13 digits long, not 15 or 16, so it could be a VISA card, but the results says its an AMEX card. I think it is a bad thing to let the switch statement drop through all cases if, lets say, the credit card number has 13 digits but doesn't fit the next if statement. But we can get is smaller anyway (in a second). I would use functions here to get the body of the switch statement smaller.
CREDIT CARD VALIDATOR LUHN ALGORITHM JAVASCRIPT CODE
I prefer if-then-else statements, but I think this is ok here, even though it is very heavy because of all the code in there. Here I would use a function called getNumberOfDigits(long long int number), so you don't have to think about your for-loop or my ceil-log10-thing. You only need to call ceil for it: digits = ceil(log10(credit_card)) It returns a floating point number which is close to the number of digits. You can read something about log10 or just play a little around. The way you solved this problem is strait forward, but I think it would be more readable with a log10-aproach. Well, but for the future I give you some quotes, why I would use a function in some places.įirst of all, you try to determine the number of digits of the credit card number. I would complain that you didn't use functions to encapsulate functionality. It could be very difficult to give you good hints, because of your restrictions. The way it is now, I think I may have over-complicated the process a bit. Having said all that, I am wondering if there is maybe a more elegant way to do all the checking involved in my program. I focused mainly on code readability, sometimes on expense of exactness.This assignment is fairly early in the course, so we are only supposed to use the most basic tools of C.the function GetLongLong() is part of that library, and I currently don't know all the details of how this function works.The cs50.h library is custom-made by the Harvard staff.performs the necessary tasks on every other digit, starting from the next-to-last one. looping through all the digits, decreasing credit_card on every iterationįor (int i = 1 i <= digits i++, credit_card /= 10) Int odd_sum = 0, even_sum = 0, total = 0 used for summing up the digits of the credit card number.

sets bank to invalid if it doesn't satisfy any of the standards. (credit_card / (long long int) pow(10, digits - 2)) <= 55)Įlse if ( (credit_card / (long long int) pow(10, digits - 1) ) = 4) If ( ( (credit_card / (long long int) pow(10, digits - 2)) >= 51) & (credit_card / (long long int) pow(10, digits - 2)) = 37)

If ( ( (credit_card / (long long int) pow(10, digits - 2)) = 34 ) || If ( (credit_card / (long long int) pow(10, digits - 1) ) = 4) check if number length corresponds to valid format * credit card number (only American Express, Visa andįor (long long int temporary = credit_card temporary > 0 * This program checks whether an entered number is a valid The validation is done with Luhn's algorithm. For the purposes of this assignment, I am only interested in Visa, American Express and MasterCard number formats. I am supposed to write a program (in C), that takes as input a long long int from the user and proceeds to check whether the entered number is a valid credit card number. I started following Harvard's CS50 (Introduction to Computer Science) on edX, and as part of their Hacker edition set 1 was the following assignment:
