# Pset1 valid visa number as invalid

I built the first part to check whether a number is a valid AMEX number and used the same logic to check for Visa number but the codes don't seem to work as I tried with several valid visa numbers and the result was always "invalid'. Would greatly appreciate your help. (After some research I understand my method is not the cleanest but still think it can work.)

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

int main(void)

{

long long n;

int A = 0;

int B = 0;

int C = 0;

int D = 0;

int E = 0;

int F = 0;

int G = 0;

int H = 0;

int I = 0;

int J = 0;

int K = 0;

int L = 0;

int M = 0;

int N = 0;

int O = 0;

int P = 0;

int S1 = 0;

do

{

}

while ( n < 0);

//check if it's a valid amex card

if ((n / 10000000000000 == 34) || (n / 10000000000000 == 37))

{

// get every other digit, multiply them by 2

N = (n / 10 % 10) * 2;

if (N == 10)

{

N = 1;

}

else if (N > 10)

{

N = (N / 10) + (N % 10);

}

L = (n / 1000 % 10) * 2;

if (L == 10)

{

L = 1;

}

else if (L > 10)

{

L = (L / 10) + (L % 10);

}

J = (n / 100000 % 10) * 2;

if (J == 10)

{

J = 1;

}

else if (J > 10)

{

J = (J / 10) + (J % 10);

}

H = (n / 10000000 % 10) * 2;

if (H == 10)

{

H = 1;

}

else if (H > 10)

{

H = (H / 10) + (H % 10);

}

F = (n / 1000000000 % 10) * 2;

if (F == 10)

{

F = 1;

}

else if (F > 10)

{

F = (F / 10) + (F % 10);

}

D = (n / 100000000000 % 10) * 2;

if (D == 10)

{

D = 1;

}

else if (D > 10)

{

D = (D / 10) + (D % 10);

}

B = (n / 10000000000000 % 10) * 2;

if (B == 10)

{

B = 1;

}

else if (B > 10)

{

B = (B / 10) + (B % 10);

}
//add the digits of the numbers multiplied by 2 together

S1 = B + D + F + H + J + L + N;

// find the digits that are not multiplied by 2

O = n % 10;

M = n /100 % 10;

K = n /10000 % 10;

I = n /1000000 % 10;

G = n /100000000 % 10;

E = n /10000000000 % 10;

C = n /1000000000000 % 10;

A = n /100000000000000 % 10;

// add the S1 and the other sum of the rest of the digits together

if ((S1 + O + M + K + I + G + E + C + A) % 10 == 0)

{

printf("AMEX\n");

}

else

{

printf("INVALID\n");

}

}

//check if it is a valid visa number

else if (n / 1000000000000 == 4)

{

// get every other digit, mutiply them by 2

L = (n / 10 % 10) * 2;

if (L == 10)

{

L = 1;

}

else if (L > 10)

{

L = (L / 10) + (L % 10);

}

J = (n / 1000 % 10) * 2;

if (J == 10)

{
J = 1;

}

else if (J > 10)

{

J = (J / 10) + (J % 10);

}

H = (H / 100000 % 10) * 2;

if (H == 10)

{

H = 1;

}

else if (H > 10)

{

H = (H / 10) + (H % 10);

}

F = (n / 10000000 % 10) * 2;

if (F == 10)

{

F = 1;

}

else if (F > 10)

{

F = (F / 10) + (F % 10);

}

D = (n / 1000000000 % 10) * 2;

if (D == 10)

{

D = 1;

}

else if (D > 10)

{

D = (D / 10) + (D % 10);

}

B = (B / 100000000000 % 10) * 2;

if (B == 10)

{

B = 1;

}

else if (B > 10)

{

B = (B / 10) + (B % 10);

}

//add the digits of the numbers mutiplied by 2 together

S1 = B + D + F + H + J + L;

// find the digits that are not multiplied by 2

M = n % 10;

K = n /100 % 10;

I = n /10000 % 10;

G = n /1000000 % 10;

E = n /100000000 % 10;

C = n /10000000000 % 10;

A = n /1000000000000 % 10;

// add the S1 and the other sum of the rest of the digits together

if ((S1 + M + K + I + G + E + C + A) % 10 == 0)

{

printf("VISA\n");

}

else

{

printf("INVALID\n");

}

}

return 0;

}
``````
• Could you maybe write that using loops, and less variables and `if`s? Pretty hard to read. – Blauelf Nov 6 '18 at 10:01
• thanks a lot! i used loops and it works! – Ching Nov 8 '18 at 18:44

Depends on which "valid VISA" numbers you are testing. This algorithm will only work on 13 digit VISA numbers. Why? Because of this: `else if (n / 1000000000000 == 4)`. Remember from the spec:
Try it with `4111111111119`. (That is a valid 13-digit VISA number).