A **credit card** number is not a sequence of random numbers.There is an algorithm for checking if it is correct or not .

The algorithm which is used in credit card validation is called the **Luhn algorithm ** or **Luhn formula**, also known as the **“modulus 10″** or **“mod 10”** algorithm .

Suppose that we are going to check the credit card number **371449635398431** .Here is the step by step guide to check if a credit card number is valid or not.

**1- Check for length-**The length of a credit card number lies between **12** to **19.**So a credit card number which has more than 19 numbers or less than 12 numbers is invalid.In my example the length is 15 characters so it passes the first test.

**2- Check for digits-**A valid credit card number contains only **0-9** digits.In given credit card number only numbers are appearing so it passes the second test.

**3-Check validity using Modulus 10 alorithm-**This is the most complex part of this tutorial so we can divide the entire process into several sub-processes.

**a).**Starting from right to left multiply each digit by 2.

(3*2),(8*2),(3*2),(3*2),(9*2),(4*2),(7*2)

6,(16),6,6,(18),8,(14)

If the number is two digits then add both digits-

6,(6+1),6,6,(1+8),8,(1+4)

6,7,6,6,9,8,5

Now add these numbers 6+7+6+6+9+8+5=47

**b).**Take the digits of credit card which are not multiplied by 2 in step** a **and calculated the sum.

3+1+4+6+5+9+4+1=33

**c).**Add the numbers which are obtained by step **a** and step **b.**

47+33=80

These three steps (a,b and c) can be easily understand by this table-

CC number | 3 | 7 | 1 | 4 | 4 | 9 | 6 | 3 | 5 | 3 | 9 | 8 | 4 | 3 | 1 | x |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

Double every second | 3 | 14 |
1 | 8 |
4 | 18 |
6 | 6 |
5 | 6 |
9 | 16 |
4 | 6 |
1 | x |

Sum of digits | 3 | 5 | 1 | 8 | 4 | 9 | 6 | 6 | 5 | 6 | 9 | 7 | 4 | 6 | 1 | =80 |

**4-**Divide the number which is getting after **step 3** by **10**.

80/10=8 and remainder=0

**If the remainder is 0 then credit card number is valid otherwise it is invalid.**