I was doing the credit problem with the visa test card in the page: 4003600000000014
It passed that test, however in other tests it doesn't pass. I think it's a matter of the sum that it can't compute it correctly so when using modulo 10 doesn't equal 0 and prints invalid.. Also I don't know if I did the get type if statements incorrectly but I don't get how. Please help :)
Here's my code
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
// Input card number
long cardNumber;
do
{
cardNumber = get_long_long("Number: ");
}
while(cardNumber < 0);
// Card calculation
int power = 0;
int sum = 0;
long beginNumber = (cardNumber / 10); // No modify
long number = beginNumber;
while (power <= 14)
{
// If only one number sum it
if ( ( ( ((number%10)*2) / 10) % 10) == 0)
{
sum += ((number % 10) * 2);
power += 2;
number = beginNumber / pow(10,power);
}
// Sum both numbers
else if ( ( ( ((number%10)*2) / 10) % 10) == 1)
{
sum += (number % 10) * 2 % 10;
sum += (number % 10) * 2 / 10 % 10;
power += 2;
number = beginNumber / pow(10,power);
}
}
number = cardNumber;
power = 0;
while (power <= 14)
{
sum += (number % 10);
power += 2;
number = number / pow(10,power);
}
// Is it legit?
if (sum % 10 != 0)
{
printf("INVALID\n");
}
else if (sum % 10 == 0)
{
// Get type
int visa_1 = cardNumber / pow(10, 15);
int visa_2 = cardNumber / pow(10, 12);
int mastercard = cardNumber / pow(10, 14);
int amex = cardNumber / pow(10, 13);
if ( visa_1 || visa_2 == 4 )
{
printf("%s\n", "VISA");
}
else if ( mastercard == (51 | 52 | 53 | 54 | 55) )
{
printf("%s\n", "MASTERCARD");
}
else if ( amex == (34 | 37) )
{
printf("%s\n", "AMEX");
}
}
}