0

I was trying out credit and tried to write a code for all 15 digit cards so if a AMEX card exists it should work in theory. However even with valid cards I'm getting the input invalid. I have not written any loops just used operators for a 15 digit card number. I know this is bad practice but I am totally stuck and thought I'll try to write down the entirety of the code which I want to kind of put in a loop to get ideas.

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
    long credit_number;
do
{
    credit_number = get_long("Number: ");
}
while (credit_number < 0);

long checksum_even_amex = ((long)2*(credit_number % 100 - credit_number % 10)/10)/10 + ((long)2*(credit_number % 100 - credit_number % 10)/10) % 10 + ((long)2*(credit_number % 10000 - credit_number % 1000)/1000)/10 + ((long)2*(credit_number % 10000 - credit_number % 1000)/1000) % 10  + ((long)2*(credit_number % 1000000 - credit_number % 100000)/100000)/10 + ((long)2*(credit_number % 1000000 - credit_number % 100000)/100000) % 10 + ((long)2*(credit_number % 100000000 - credit_number % 10000000)/10000000) % 10 + ((long)2*(credit_number % 10000000000 - credit_number % 1000000000)/1000000000)/10 + ((long)2*(credit_number % 10000000000 - credit_number % 1000000000)/1000000000) % 10 + ((long)2*(credit_number % 100000000000 - credit_number % 100000000000)/100000000000)/10 + ((long)2*(credit_number % 100000000000 - credit_number % 100000000000)/100000000000) % 10 + ((long)2*(credit_number % 10000000000000 - credit_number % 10000000000000)/10000000000000)/10 + ((long)2*(credit_number % 10000000000000 - credit_number % 10000000000000)/10000000000000) % 10;
long checksum_odd_amex = (credit_number % 10)                           + (credit_number % 1000 - credit_number % 100)/100    + (credit_number % 10000 - credit_number % 1000)/10000      + (credit_number % 1000000 - credit_number % 100000)/1000000      + (credit_number % 100000000 - credit_number % 10000000)/100000000      + (credit_number % 10000000000 - credit_number % 1000000000)/10000000000     + (credit_number % 1000000000000 - credit_number % 100000000000)/1000000000000 + (credit_number % 100000000000000 - credit_number % 10000000000000)/100000000000000;
long net_checksum_amex = checksum_even_amex + checksum_odd_amex;
if (net_checksum_amex % 10 == 0)
{
    printf("AMEX\n");
}
else
{
    printf("INVALID");
}
}
1

This is because you've overlooked an important piece of information provided with the explanation of Luhn's algorithm. It actually says in the algorithm to add the products' digits to the checksum and not the product itself(which you've done).

Here's an excerpt from the pset1 page for your reference:

According to Luhn’s algorithm, you can determine if a credit card number is (syntactically) valid as follows:

  • Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.
  • Add the sum to the sum of the digits that weren’t multiplied by 2.
  • 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!

Also, if you want to avoid brute force, you may use pow() function which comes with math library of C(also which you've already included in your code) and then try to put it in a loop. Good Luck!

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .