Credit Cards (Lun MOD 10)

The length of a credit card can be either 13 or 15 or 16 characters. The first digit is called the MIF or the major industry identifier. The various values are as follows.

0

ISO/TC 68 and Other Industries

1

Airlines

2

Airlines and Other Industries

3

Travel and Entertainment

4

Banking and Financial

5

Banking and Financial

6

Merchandizing and Banking

7

Petroleum

8

Telecommunications and Other Industries

9

National ( Local Use )

Most of us will carry credit cards beginning with 4 or 5. A petrol card will begin with 7. The first 4 to 6 digits are called the issuers code and they tell us which entity issued us the card. Some well known issuers are as.

Issuer's Code
The first four digits

Card Issuer

Number
of Digits

3000 to 3059
3600 to 3699
3800 to 3889

Diners Club

14

3400 to 3499
3700 to 3799

American Express

15

3528 to 3589

JCB

16

3890 to 3899

Carte Blanche

14

4000 to 4999

Visa

13 or 16

5100 to 5599

MasterCard

16

5610

Australian BankCard

16

6011

Discover / Novus

16

The remaining 10-12 bits are the actual credit card number. These numbers are not just randomly generated by the credit card companies but are generated such that they meet a certain rule. This rule or algorithm called the Luhn algorithm. The International Standards Organization (ISO/IEC 7812-1:1993) and the American National Standards Institute (ANSI X4.13) have now standardized on the above algorithm for credit card number validation.

Lets take a dummy Visa card and apply the Luhn algorithm to it.

4567 1234 5678 9129

We first start from the right and this last digit is called the check sum. We start with the rightmost –1 digit and multiply every other  digit by 2.

4 5 6  7     1 2  3   4  5     6   7    8   9   1   2  9

8   12        2     6     10        14        18       4

We then subtract 9 from any number larger than 10. Thus we get.

8 3 2 6 1 5 9 4.

Another  way out is by adding the individual digits.

We then add them all up to get

38

We then add all the other digits to get

42

The sum of the two is 80 which will add up to a number divisible by 10. Thus the above algorithm is called the Luhn modulus 10. The last digit is called the check digit as it is the one used to make sure that we get a final number divisible by 10. We have a 90% reliability with the Luhn algorithm.

a.c
void main (argc, argv)
int  argc;
char **argv;
{
int i,=0,t,len;
char *u = argv[1];
len=strlen(u);
for(i = 0; i < len ; i++)
{
t = u[len - i - 1] - '0';
if ( (i % 2) != 0)
{
t = t * 2;
t = t < 10 ? t : t - 9;
}
s += t;
}
printf("%d\n",s % 10);
}
 
cl a.c
 
a 4567123456789129

The above program is to be run with a parameter that is the credit card number. We use u as a pointer to this string of digits. The variable len will contain the length of the credit card digits, normally 16. We now iterate in a for loop depending upon n where I will start from 0 to len.

We use the array notation to first pick up the 15 th digit assuming a 16 digit credit card. As len is 16, I is 0, the array subscript will be 15. Remember in C, arrays start from 0 and u[0] is 4 and u[15] is 9. Thus  as the array members are actually numbers from 0 to 9, we subtract from ‘0’ or 48 to get at the actual number.

Thus u[15] now becomes actual number 9 which we store in variable t. We then use the modulus % operator to get at the remainder after dividing by 2. If the number is divisible by 2, the remainder is 0 else it will be 1. The remainder after doing a mod of 2 will be 0 or 1. Thus if I%2 is zero, we have hit upon a even value of i.

As the last or 15 h digit is odd, we simple add the value of t to s, which will store the running total. The second last digit u[14] is 2 and as I%2 is 0, the if statement is true. We simply multiply t by 2 and then use the ? : ternary operator to subtract 9 if the number is larger than 9.

We then always add this value of t to s. When we quit out of the, for loop, we display the mod of s with 10 which should be 0.

Back to the main page