# credit pset1 cs50 sum while loop

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");
}
}
}
``````

A problem I found is at line 47 of the code (the second while loop):

``````while (power <= 14)
{
sum += (number % 10);
power += 2;
number = number / pow(10,power);
}
``````

I think you meant the line `number = number / pow(10,power)` to be:

``````number = beginningNumber / pow(10,power)
``````

Otherwise the variable `number` does the following (during the second while loop):

``````iteration             "number"           "power"
0                    378282246310005      0
1                    3782822463100        2
2                    378282246            4
3                    378                  6
4                    0                    8
5                    0                   10
6                    0                   12
7                    0                   14
8                    0                   16
``````

Another problem is the way comparisons are written. In C the name of the variable has to be repeated every time instead of the way this code is written. And the OR operator should be a double pipe `||` instead of a single pipe `|`:

``````if ( visa_1 || visa_2 == 4 )
``````

Each one has to be compared like this:

``````if (visa_1 == 4 || visa_2 == 4)
``````

``````else if ( mastercard == (51 | 52 | 53 | 54 | 55) )
``````else if ( amex == (34 | 37) )
Double pipes must be used as an OR operator, and compare each number (or use less than `<` and greater than signs `>` to work with a range of values):
``````else if ( amex == 34 || amex == 37 )