In this post, we will see about Luhn Algorithm in java

Table of Contents

## Introduction

`Luhn algorithm`

, also known as `modulus 10`

or `mod 10`

algorithm, is a simple checksum process for validating various identification numbers such as credit card numbers, Canadian social securities numbers.

This algorithm is designed to protect again mistyped or accidental error rather than malicious attacks. Most credit card companies adopted this algorithm as this was available in the public domain and can be used by anyone.

Here are the steps involved in Luhn Algorithms.

`Step 1:`

From the rightmost digit, we should double every second digit.

`Step 2:`

When we double the digits and get product in double digits, then we should add digits of the product.

`Step 3:`

Compute the sum of all the digits.

`Step 4:`

If total sum is divisible by 10 i.e. total sum modulo 10 is 0, then number is valid else it is not valid.

As `90 mod 10 is 0`

, hence this is valid credit card number.

## Detailed implementation

Let’s create java program to implement the Luhn algorithm.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package org.arpit.java2blog; import java.util.Arrays; public class LuhnAlgorithmMain { public static void main(String[] args) { String cardNumber="1358954993914435"; boolean validCreditCardNumber = isValidCreditCardNumber(cardNumber); if(validCreditCardNumber) { System.out.println(cardNumber+" is valid as per luhn algorithm"); } else { System.out.println(cardNumber+" is not valid as per luhn algorithm"); } } public static boolean isValidCreditCardNumber(String cardNumber) { // int array for processing the cardNumber int[] cardIntArray=new int[cardNumber.length()]; for(int i=0;i<cardNumber.length();i++) { char c= cardNumber.charAt(i); cardIntArray[i]= Integer.parseInt(""+c); } for(int i=cardIntArray.length-2;i>=0;i=i-2) { int num = cardIntArray[i]; num = num * 2; // step 1 if(num>9) { num = num%10 + num/10; // step 2 } cardIntArray[i]=num; } int sum = sumDigits(cardIntArray); // step 3 System.out.println(sum); if(sum%10==0) // step 4 { return true; } return false; } public static int sumDigits(int[] arr) { return Arrays.stream(arr).sum(); } } |

**Output:**

1358954993914435 is valid as per luhn algorithm

Here are the steps involved in above java program for luhn algorithm.

- Convert a String
`cardNumber`

to int array`cardIntArray`

for processing - Iterate
`cardIntArray`

from rightmost side starting from`cardIntArray.length-2`

with step of`i=i-2`

- Mulitply digit by 2
- If product is greater than 9, sum the product
- Assign result back to
`cardIntArray[i]`

- Once the loop is over, compute sum of elements of
`cardIntArray`

- if
`sum%10`

is`0`

then credit card number is valid else not valid as per luhn algorithm

## Using Apache common validation

In case, you don’t want to implement luhn algorithm you can use Apache common validation library and validate credit card number using `LuhnCheckDigit.LUHN_CHECK_DIGIT.isValid(creditCardNumber);`

You need to add following dependency to `pom.xml`

.

1 2 3 4 5 6 7 8 |
<!-- https://mvnrepository.com/artifact/commons-validator/commons-validator --> <dependency> <groupId>commons-validator</groupId> <artifactId>commons-validator</artifactId> <version>1.6</version> </dependency> |

Let’s see with the help of example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package org.arpit.java2blog; import org.apache.commons.validator.routines.checkdigit.LuhnCheckDigit; public class LuhnAlgorithmApache { public static void main(String[] args) { String cardNumber="79927398713"; boolean validCreditCardNumber = isValidCreditCardNumberLuhn(cardNumber); if(validCreditCardNumber) { System.out.println(cardNumber+" is valid as per luhn algorithm"); } else { System.out.println(cardNumber+" is not valid as per luhn algorithm"); } } public static boolean isValidCreditCardNumberLuhn(String creditCardNumber) { return LuhnCheckDigit.LUHN_CHECK_DIGIT.isValid(creditCardNumber); } } |

**Output:**

That’s all about Luhn Algorithm in java