What is Luhn Algorithm?

Luhn algorithm has been developed to decrease probability of entering wrong identification numbers. The last digit is a checksum of the other digits. It is being used in credit card numbers, IMEI numbers, social security numbers, SIM card numbers and so on. This algorithm was developed by Hans Peter Luhn.

How to calculate the check digit?

  1. If the number already contains the check digit, remove that digit. The check digit is the last digit.
  2. Start from the rightmost digit. Moving left, double the value of every second digit (including the rightmost digit).
  3. Sum up the digits of the resulting value in each position (using the original value where a digit did not get doubled in the previous step).
  4. The check digit is calculated by 10 − (s mod 10).