The checksum is correct for less than 10 digits but for 14-16 digits it is coming back negative. What am I doing wrong?
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int digit(long number, int place)
{
int k = number / pow(10, place - 1) ;
int final = k % 10;
return final;
}
int numberofdigits(long nmber)
{
long m = log10(nmber);
long u = floor(m);
int e = u + 1;
return e;
}
int checksum(long ccn)
{
int k = 0;
for (int i = 2; i<numberofdigits(ccn); i = i + 2)
{
if (digit(ccn, i) < 5)
{
k = k + digit(ccn, i) * 2;
}
else
{
k = k + (digit(ccn, i) * 2) % 10 + digit(digit(ccn, i) * 2, 2);
}
}
for (int i = 1; i < numberofdigits(ccn); i = i + 2)
{
k = k + digit(ccn, i);
}
return k;
}
int main(void)
{
long input = get_long("Number: ");
if ((numberofdigits(input) == 16 || numberofdigits(input) == 13) && (digit(input, numberofdigits(input)) == 4) && (checksum(input)%10 == 0))
{
printf("VISA\n");
}
else if (numberofdigits(input) == 15 && digit(input, numberofdigits(input)) == 3 && (digit(input, (numberofdigits(input)-1)) == 4 || digit(input, (numberofdigits(input)-1)) == 7) && checksum(input) % 10 == 0)
{
printf("AMEX\n");
}
else if (numberofdigits(input) == 16 && digit(input, numberofdigits(input)) == 5 && (digit(input, numberofdigits(input)-1) == 1 || digit(input , numberofdigits(input) - 1) == 2 || digit(input , numberofdigits(input)-1) == 3 ||digit(input, numberofdigits(input)-1) == 4 || digit(input, numberofdigits(input) - 1) == 5 ) && checksum(input) % 10 == 0)
{
printf("MASTERCARD\n");
}
else
{
printf("INVALID\n");
}
}