# pset1:credit, multiplication problem

for some reason my program doesn't multiply `(card_number % 10) * 2` for example when `card_number % 10 = 6` the output is `6` instead of `12`.

``````    for(int x = 0; card_number != 0; x++)
{
if (x % 2 == 0)
{
sum += (2 * (card_number % 10))
}
else
{
sum += (card_number % 10);
printf("%i\n", sum);
}
card_number /= 10;
printf("%lli\n", card_number);
``````

Here is the full function code which should check if a card is valid

``````string is_valid_card(long long card_number)
{
int card_len = card_length(card_number);
int sum = 0;
for(int x = 0; card_number != 0; x++)
{
if (x % 2 == 0)
{
sum += (2 * (card_number % 10))
}
else
{
sum += (card_number % 10);
printf("%i\n", sum);
}
card_number /= 10;
printf("%lli\n", card_number);
}
if(sum % 10 == 0)
{
if (card_len == 13)
{
return "VISA\n";
}
else if (card_len == 15)
{
return "AMEX\n";
}
else
{
return "MASTERCARD\n";
}
}
else
{
return "INVALID";
printf("%i\n", sum);
}
}
``
``````

The last digit (`x` being `0`) should not be doubled. Invert the logic.

Also, it's not simply twice the last digit, if the result is two digits, those digits are added (or, you could always add the two digits, the first one would be 0 or 1). The number added would be

``````0 * 2 =  0 -> 0 + 0 = 0
1 * 2 =  2 -> 0 + 2 = 2
2 * 2 =  4 -> 0 + 4 = 4
3 * 2 =  6 -> 0 + 6 = 6
4 * 2 =  8 -> 0 + 8 = 8
5 * 2 = 10 -> 1 + 0 = 1
6 * 2 = 12 -> 1 + 2 = 3
7 * 2 = 14 -> 1 + 4 = 5
8 * 2 = 16 -> 1 + 6 = 7
9 * 2 = 18 -> 1 + 8 = 9
``````

Ten different input digits, ten different output digits. That's important, as changing one digit will always change the checksum.

• I wish i could have seen this earlier it could have saved me a whole lot of trouble since it took me two days to find that bit !!!! – kylekibet Nov 18 '19 at 14:29