ccs50luhn

Attempting to run Luhn's Algorithm in C


So, I'm trying to make Luhn's algorithm in C, but it doesn't return the correct values when running it.

    //Luhn's Algorithm
    int tsum = 0;
    if (count % 2 != 0)
    {
        for (int tempcount = count; tempcount > 0; tempcount--)
        {
            if (tempcount % 2 != 0)
            {
                tsum += (cardNum % 10);
            }
            else
            {
                tsum += (cardNum % 10)*2;
            }
            cardNum /= 10;
        }
    }
    else
    if (count % 2 == 0)
    {
        for (int tempcount = count; tempcount > 0; tempcount--)
        {
            if (tempcount % 2 == 0)
            {
                tsum += (cardNum % 10);
            }
            else
            {
                tsum += (cardNum % 10)*2;
            }
            cardNum /= 10;
        }

    }
    tsum %= 10;

I have spent hours trying to troubleshoot and find the issue, count represents the number of digits in the card number, the rest define themselves.

I would appreciate somebody to tell me what I'm doing wrong, thank you.

EDIT: Apologies, cardNum is a long long. And I am expecitng a value of 0 for tsum when inputting any card number from Paypal's Standard Test Cards. I would also like to add, in my mind what this code should do is: using the checks for even and odd it will start from the rightmost digit and add it to tsum, every other digit will do the same. Then starting from the 2nd rightmost digit, the digit is multiplied by 2 then added to tsum, with every other digit doing the same. Then, the last line will check the rightmost digit of the current tsum and will tell me if the card is valid according to Luhn's Algorithm.


Solution

  • Thank you for your help, I was able to solve this issue as it seems I had missed a step in Luhn's Algorithm.

        //Luhn's Algorithm
        int tsum = 0;
        int luhn;
        if (count % 2 != 0)
        {
            for (int tempcount = count; tempcount > 0; tempcount--)
            {
                if (tempcount % 2 != 0)
                {
                    luhn = (cardNum % 10);
                    tsum += luhn;
                }
                else
                {
                    luhn = (cardNum % 10) * 2;
                    if (luhn > 9)
                    {
                        tsum += (luhn - 9);
                    }
                    else
                    {
                        tsum += luhn;
                    }
                }
                cardNum /= 10;
            }
        }
        else if (count % 2 == 0)
        {
            for (int tempcount = count; tempcount > 0; tempcount--)
            {
                if (tempcount % 2 == 0)
                {
                    luhn = (cardNum % 10);
                    tsum += luhn;
                }
                else
                {
                    luhn = (cardNum % 10) * 2;
                    if (luhn > 9)
                    {
                        tsum += (luhn - 9);
                    }
                    else
                    {
                        tsum += luhn;
                    }
                }
                cardNum /= 10;
            }
    
        }
        tsum %= 10;
    

    Here, I defined luhn as the next integer to be added to the tsum, in doing this I was able to fill in the missing step by subtracting 9 before adding to tsum if luhn exceeded 9.