# Code check for bad credit pset1

This is my attempt for the bad credit problem in pset1 and I'm having trouble debugging my code. Any help would be appreciated.

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

bool checksum(long long number);

int main(void)
{
long long number,i;
int count, first_dig, first_two;

printf ("Number: ");
number = get_long_long();

for (count = 1, i = number; i >= 100; count++)
{
i = i/10;
}

first_two = i;
first_dig = i/10;
++count;

if (count < 13 || count > 16 || count == 14)
{
printf("INVALID\n");
}

if ((count == 13 || count == 16) && first_dig == 4 && checksum(number) == true)
printf ("VISA\n");

else if ( (count == 15) && (first_two == 34 || first_two == 37) && checksum(number) == true )
printf ("AMEX\n");

else if( (count == 16) && (first_two == 51 || first_two == 52 || first_two == 53 || first_two == 54 || first_two == 55 ) && checksum(number) == true)
printf ("MASTERCARD\n");

else printf ("INVALID\n");

}

bool checksum(long long number)
{
long long value = number;
int i, j, digits[20], sum = 0;

for (i = 0; value > 0; ++i)
{
digits[i] = value % 10;
value /= 10;

}

for (j = 0; j < i; j++)
{
if ( (j % 2) == 0)
{
sum += digits[j];
}

else sum += (digits[j] * 2);
}

if (sum % 10 == 0)
{
return true;
}

else return false;
}

• Welcome! As a new user, let me give you some friendly guidance. When you have a question, please include an accurate description of the problem(s) you're having so that people have an idea what to look at. (Nobody really wants to have to find the problem and suggest solutions.) Also, check out the following link to see what not to do. Then, please edit your question, adding a description of the problem with some details. Again, welcome to CS50x! meta.cs50.stackexchange.com/questions/63/… Jan 3, 2017 at 7:02

sum += (digits[j] * 2);


is not how the algorithm works. For digits 0..4, it's that, but for 5..9, you add the two digits of the result, so it's

sum += (digits[j] * 2) - 9;


you can combine that for example to

sum += digits[j] * 2 - (digits[j] > 4) * 9;


or

sum += digits[j] * 2 - (digits[j] > 4 ? 9 : 0);


or

sum += (digits[j] % 5) * 2 + digits[j] / 5;


or some different variation