I am working on the credit problem of CS50. However, I am only printing INVALID no matter what card number I put in. May I ask what is the problem with my code? It seems that there is something wrong with the part to calculate the total sum.
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
// Get the card number
long num;
do
{
num = get_long("What is the card number?\n");
} while (num < 0);
long sum = 0, sum2 = 0, count = 0;
//Get the sum
for (long i = num; i > 0; i = i / 10)
{
sum += i % 10;
count++;
}
for (long i = num / 10; i > 0; i = i / 100)
{
sum2 += i % 10;
}
if ((sum + sum2) % 10 != 0)
{
printf("INVALID");
}
else
{
long digits = num / (10 * (count - 2));
if (count == 15 &&
(digits == 34 || digits == 37))
{
printf("AMERICAN EXPRESS");
}
else if (count == 16 && 51 <= digits <=55)
{
printf("MASTERCARD");
}
else if ((count == 16 || count == 13) && (digits / 10) == 4)
{
printf("VISA");
}
else
{
printf("INVALID");
}
}
}