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


int main (void)
{
    //User's Input
    long n = get_long("Number: ");

    int sum_of_odds = 0;
    int sum_of_evens = 0;
    int count = 0;
    int digit;
    long temp = n;

    //Computation of numbers(Luhn’s Algorithm)
    for (count = 0; temp == 0; count++)
    {
        digit = temp % 10;
        temp /= 10;
        if (count % 2 == 0)
        {
            sum_of_odds += digit;
        }
        else
        {
            digit *= 2;
            if (digit >= 10)
            {
                sum_of_evens += digit - 10 + 1;
            }
            else
            {
                sum_of_evens += digit;
            }
        }
    }

    int start;

    //Validity
    if ((sum_of_odds + sum_of_evens) % 10 == 0)
    {
        if (count == 15)
        {
            start = n / 10^13;
            if (start == 34 || n == 37)
            {
                printf("AMEX\n");
            }
            else
            {
                printf("INVALID\n");
            }
        }
        else if (count == 16)
        {
            start = n / 10^14;
            if (start == 51 || start == 52 || start == 53 || start == 54 || start == 55)
            {
                printf("MASTERCARD\n");
            }
            else if (start / 10 == 4)
            {
                printf("VISA\n");
            }
            else
            {
                printf("INVALID\n");
            }
        }
        else if (count == 13)
        {
            start = n / 10^12;
            if (start == 4)
            {
                printf("VISA\n");
            }
            else
            {
                printf("INVALID\n");
            }
        }
        else
        {
            printf("INVALID\n");
        }
    }
    else
    {
        printf("INVALID\n");
    }
}

I seem to have messed up in summing up under Luhn's Algorithm as I tested and printed out a sum of 0 after the computation. Could anyone help me identify my errors made. Thank you in advance!

0

The problem I see is with the declaration of your for loop. temp == 0 in the condition/second part of the declaration means that the loop will only execute if this is true. I do not think this is what you want to happen. Also, because your code only ever has temp /= 10 and temp is declared as a long, I am pretty sure it will never equal zero. This means that changing temp == 0 to temp >= 0 is not going to be a quick solution to your issue, either, since it will cause an infinite loop.

What you will need to do is decide what you want to be the exit condition for the loop (the thing that tells the program it is time to stop executing it) and then ensure that your code makes that condition possible.

Hope this helps. If it does, feel free to click the check mark next to my answer to accept it. But if not, let me know and I will try to help some more.

You must log in to answer this question.

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