Hi there, just written a Luhn's algo programme. I have no previous experience in C (except for last couple of weeks) so I'm aware that my code will be unwieldy and quite simple. How could I have made this better?
Is there any way I could have grouped the letter int variable, for example, something like:
Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
long ccnum = 0;
long AMEX;
long VISA;
long MASTERCARD;
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p = 0;
int a1, a2, b1, b2, c1, c2, d1, d2, e1, e2, f1, f2, g1, g2, h1, h2 = 0;
int cksum;
do
{
ccnum = get_long("Enter Number:");
if (ccnum <= 4000000000000 || ccnum >= 5999999999999999)
{
printf("INVALID\n");
return 0;
}
if (ccnum >= 300000000000000 && ccnum <= 339999999999999)
{
printf("INVALID\n");
return 0;
}
if (ccnum >= 300000000000000 && ccnum <= 369999999999999)
{
printf("INVALID\n");
return 0;
}
if (ccnum >= 5000000000000000 && ccnum <= 5099999999999999)
{
printf("INVALID\n");
return 0;
}
if (ccnum >= 5600000000000000)
{
printf("INVALID\n");
return 0;
}
}
while (ccnum < 4000000000000 || ccnum > 5999999999999999);
a = ccnum % 100 / 10 * 2;
b = ccnum % 10000 / 1000 * 2;
c = ccnum % 1000000 / 100000 * 2;
d = ccnum % 100000000 / 10000000 * 2;
e = ccnum % 10000000000 / 1000000000 * 2;
f = ccnum % 1000000000000 / 100000000000 * 2;
g = ccnum % 100000000000000 / 10000000000000 * 2;
h = ccnum % 10000000000000000 / 1000000000000000 * 2;
i = ccnum % 10;
j = ccnum % 1000 / 100;
k = ccnum % 100000 / 10000;
l = ccnum % 10000000 / 1000000;
m = ccnum % 1000000000 / 100000000;
n = ccnum % 100000000000 / 10000000000;
o = ccnum % 10000000000000 / 1000000000000;
p = ccnum % 1000000000000000 / 100000000000000;
if (a >= 10)
{
a1 = a % 10;
a2 = a / 10;
a = a1 + a2;
}
if (b >= 10)
{
b1 = b % 10;
b2 = b / 10;
b = b1 + b2;
}
if (c >= 10)
{
c1 = c % 10;
c2 = c / 10;
c = c1 + c2;
}
if (d >= 10)
{
d1 = d % 10;
d2 = d / 10;
d = d1 + d2;
}
if (e >= 10)
{
e1 = e % 10;
e2 = e / 10;
e = e1 + e2;
}
if (f >= 10)
{
f1 = f % 10;
f2 = f / 10;
f = f1 + f2;
}
if (g >= 10)
{
g1 = g % 10;
g2 = g / 10;
g = g1 + g2;
}
if (h >= 10)
{
h1 = h % 10;
h2 = h / 10;
h = h1 + h2;
}
cksum = a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p;
if (ccnum >= 340000000000000 && ccnum <= 349999999999999 && cksum % 10 == 0)
{
printf("AMEX\n");
return 0;
}
if (ccnum >= 370000000000000 && ccnum <= 379999999999999 && cksum % 10 == 0)
{
printf("AMEX\n");
return 0;
}
if (ccnum >= 5100000000000000 && ccnum <= 5599999999999999 && cksum % 10 == 0)
{
printf("MASTERCARD\n");
return 0;
}
if (ccnum >= 4000000000000 && ccnum <= 4999999999999 && cksum % 10 == 0)
{
printf("VISA\n");
return 0;
}
if (ccnum >= 4000000000000000 && ccnum <= 4999999999999999 && cksum % 10 == 0)
{
printf("VISA\n");
return 0;
}
else
{
printf("INVALID\n");
return 0;
}
}