Jan 262017

# Luhn algorithm implementation in Java

• 26 January 2017 The Luhn algorithm ("modulus 10" or "mod 10" algorithm, Luhn formula) is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers (PAN) or IMEI numbers. The algorithm is specified in ISO/IEC 7812-1. The algorithm was designed to protect against accidental errors. The Luhn algorithm will detect any single-digit error, as well as almost all transpositions of adjacent digits. It will not, however, detect transposition of the two-digit sequence 09 to 90 (or vice versa).

## Description

### 1. Calculate the check digit

The check digit is obtained by computing the sum of the non-check digits then computing 9 times that value modulo 10. In algorithm form:

• From the rightmost digit, which is the check digit, moving left, double the value of every second digit;
• If the product of this doubling operation is greater than 9, then sum the digits of the products or alternatively subtract 9 from the product.
• Take the sum of all the digits.
• Multiply by 9.
• The last digit is the check digit.

#### Example

##### Initial PAN
 PAN 3 7 1 4 4 9 6 3 5 3 9 8 4 3 X
##### Step 1 - double the values
 Double values 3 14 1 8 4 18 6 6 5 6 9 16 4 6 X
##### Step2 - sum the digits
 Sum digits 3 5 1 8 4 9 6 6 5 6 9 7 4 6 X
##### Step3 - sum all digits
3 + 5 + 1 + 8 + 4 + 9 + 6 + 6 + 5 + 6 + 9 + 7 + 4 + 6 = 79
79 x 9 = 711
##### Result

The check digit is 1.

If you want to calculate online the check digit you can try this calculator.

### 2. Validate the check digit

• From the rightmost digit, which is the check digit, moving left, double the value of every second digit;
• if the product of this doubling operation is greater than 9,then sum the digits of the products or alternatively subtract 9 from the product.
• Take the sum of all the digits.
• If the total modulo 10 is equal to 0 then the number is valid according to the Luhn formula; else it is not valid.

#### Example

##### Initial pan
 PAN 3 7 1 4 4 9 6 3 5 3 9 8 4 3 1
##### Step 1 - double the values
 Double values 3 14 1 8 4 18 6 6 5 6 9 16 4 6 1
##### Step2 - sum the digits
 Sum digits 3 5 1 8 4 9 6 6 5 6 9 7 4 6 1
##### Step3 - sum all digits
3 + 5 + 1 + 8 + 4 + 9 + 6 + 6 + 5 + 6 + 9 + 7 + 4 + 6 + 1= 80
80 % 10 = 0
##### Result

The pan number is valid.

If you want to validate online a PAN number you can try this validator.

### Implementation

Here is the Luhn algorithm Java implementation with in-line comments.

``````package com.admfactory.cards;

public class Cards {
/**
* Checks if the card is valid
*
* @param card
* @return result {@link boolean} true of false
*/
public static boolean luhnCheck(String card) {
if (card == null)
return false;
char checkDigit = card.charAt(card.length() - 1);
String digit = calculateCheckDigit(card.substring(0, card.length() - 1));
return checkDigit == digit.charAt(0);
}

/**
* Calculates the last digits for the card number received as parameter
*
* @param card
* @return {@link String} the check digit
*/
public static String calculateCheckDigit(String card) {
if (card == null)
return null;
String digit;
/* convert to array of int for simplicity */
int[] digits = new int[card.length()];
for (int i = 0; i < card.length(); i++) {
digits[i] = Character.getNumericValue(card.charAt(i));
}

/* double every other starting from right - jumping from 2 in 2 */
for (int i = digits.length - 1; i >= 0; i -= 2)	{
digits[i] += digits[i];

/* taking the sum of digits grater than 10 - simple trick by substract 9 */
if (digits[i] >= 10) {
digits[i] = digits[i] - 9;
}
}
int sum = 0;
for (int i = 0; i < digits.length; i++) {
sum += digits[i];
}
/* multiply by 9 step */
sum = sum * 9;

/* convert to string to be easier to take the last digit */
digit = sum + "";
return digit.substring(digit.length() - 1);
}

public static void main(String[] args)	{
String pan1 = "37828224631000";
System.out.println("Cards luhn calculator");
System.out.println();
System.out.println("Calculate check digit for: " + pan1);
String digit = calculateCheckDigit(pan1);
System.out.println("Check digit: " + digit);
System.out.println();

String pan2 = "4012888888881881";
System.out.println("Validate pan number '" + pan2 + "': " + luhnCheck(pan2));
}

}
``````

For testing, I used cards from PaypalObjects website.The output for the tests is:

``````Cards luhn calculator

Calculate check digit for: 37828224631000
Check digit: 5

Validate pan number '4012888888881881': true
``````

Also, you can try with different cards or even with your own cards.