Skip to content

Instantly share code, notes, and snippets.

@mdp
Created March 21, 2014 17:38
Show Gist options
  • Save mdp/9691528 to your computer and use it in GitHub Desktop.
Save mdp/9691528 to your computer and use it in GitHub Desktop.
Luhn algorithm (mod10) in Java
/**
* Luhn Class is an implementation of the Luhn algorithm that checks validity of a credit card number.
*
* @author <a href="http://www.chriswareham.demon.co.uk/software/Luhn.java">Chris Wareham</a>
* @version Checks whether a string of digits is a valid credit card number according to the Luhn algorithm. 1. Starting with the second to last digit and
* moving left, double the value of all the alternating digits. For any digits that thus become 10 or more, add their digits together. For example,
* 1111 becomes 2121, while 8763 becomes 7733 (from (1+6)7(1+2)3). 2. Add all these digits together. For example, 1111 becomes 2121, then 2+1+2+1 is
* 6; while 8763 becomes 7733, then 7+7+3+3 is 20. 3. If the total ends in 0 (put another way, if the total modulus 10 is 0), then the number is valid
* according to the Luhn formula, else it is not valid. So, 1111 is not valid (as shown above, it comes out to 6), while 8763 is valid (as shown
* above, it comes out to 20).
* @param ccNumber
* the credit card number to validate.
* @return <b>true</b> if the number is valid, <b>false</b> otherwise.
*/
public class Luhn
{
public static boolean Check(String ccNumber)
{
int sum = 0;
boolean alternate = false;
for (int i = ccNumber.length() - 1; i >= 0; i--)
{
int n = Integer.parseInt(ccNumber.substring(i, i + 1));
if (alternate)
{
n *= 2;
if (n > 9)
{
n = (n % 10) + 1;
}
}
sum += n;
alternate = !alternate;
}
return (sum % 10 == 0);
}
}
@smaspe
Copy link

smaspe commented Apr 14, 2015

n = (n % 10) + 1;

could be

n -= 9;

since n is less than 10 before being multiplied by 2

@Fabrice2013
Copy link

Fabrice2013 commented Sep 29, 2017

Hi Chris. Very nice and efficient algorithm you made. The most elegant approach to Luhn I have seen yet.

@shibha
Copy link

shibha commented Jan 31, 2018

Hi, is there a library for this code that i can use in my project ?

@rabestro
Copy link

n = (n % 10) + 1;

could be

n -= 9;

since n is less than 10 before being multiplied by 2

n = 1
n *= 2 => 2
n -=9 => -7 incorrect

@StrawBerryAtWork
Copy link

n = (n % 10) + 1;

could be

n -= 9;

since n is less than 10 before being multiplied by 2

n = 1
n *= 2 => 2
n -=9 => -7 incorrect

hopefully, there is a precondition: if (n > 9)

;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment