Luhn Algorithm is simple checksum formula used to validate different identification numbers like credit card numbers, IMEI Numbers, National Provider Identifier Numbers etc.
But before understanding this algorithm you want to know what is checksum?
So checksum is nothing just a small size calculated data from a input for the purpose of detecting error which may be introduced during transmission or storage. Means it just checking procedure whether we have got our package correct or something missing there.
Now just look at the picture
Here you can see if we changing any letter there is a big difference in checksum value so if checksum value of our input and output is same that means no data missing, all is well.
I think now you have an idea of checksum. Now come to Luhn algorithm explanation
1 – From the rightmost side, (the last digit is known as check digit). Double every second number. And then sum the digits of the products
2- take the sum of all digit.
3 - if the total module 10 is 0 then the number is valid else number is invalid.
Example
5
5
2
9
4
2
0
3
5
0
6
1
5
4
6
5
5*2
5
2*2
9
4*2
2
0*2
3
5*2
0
6*2
1
5*2
4
6*2
5
10
5
4
9
8
2
0
3
10
0
12
1
10
4
12
5
1
5
4
9
8
2
0
3
1
0
3
1
1
4
3
5
Sum is 1+5+4+9+8+2+0+3+1+0+3+1+1+4+3+5 = 50
50 Mod 10 = 0
If value is 0 Then your number is valid number else number is invalid.
This is how luhn algorithm works.