Java Credit Card Validation Program using Luhn Algorithm

Recently I came to know that Credit Card numbers are not random and passes Luhn Algorithm test.

Any credit card number should pass following test:

  1. From the rightmost digit, we should double every second digit. If the double is greater than 9, then add the both digits so that final number is of single digit.
  2. Now sum all the digits in the number, the unchanged numbers and the doubled numbers.
  3. The final sum should be multiple of 10 or mod 10 of the number should be 0. If it’s not then its not a valid credit card number.

Let’s check it with an example credit card number 12345678903555.

Digits are : 1,2,3,4,5,6,7,8,9,0,3,5,5,5
After doubling : 2,2,6,4,1,6,5,8,9,0,6,5,1,5
Sum of digits : 2+2+6+4+1+6+5+8+9+0+6+5+1+5 = 60 = 6*10 and hence a valid credit card number.

Here I am providing Luhn Algorithm implementation java program to validate credit card numbers.

Java Credit Card Validation

package com.journaldev.design.test;

public class LuhnAlgoImpl {

	public static void main(String[] args) {
		validateCreditCardNumber("12345678903555");
		String imei = "012850003580200";
		validateCreditCardNumber(imei);
	}


	private static void validateCreditCardNumber(String str) {
		
		int[] ints = new int[str.length()];
		for(int i = 0;i< str.length(); i++){
			ints[i] = Integer.parseInt(str.substring(i, i+1));
		}
		for(int i = ints.length-2; i>=0; i=i-2){
			int j = ints[i];
			j = j*2;
			if(j>9){
				j = j%10 + 1;
			}
			ints[i]=j;
		}
		int sum=0;
		for(int i = 0;i< ints.length; i++){
			sum+=ints[i];
		}
		if(sum%10 == 0){
			System.out.println(str + " is a valid credit card number");
		}else{
			System.out.println(str + " is an invalid credit card number");
		}
	}

}

Here I am taking input to the validation method as String, so that it will work also where the first digit is 0.

Output of the above program is:

12345678903555 is a valid credit card number
012850003580200 is a valid credit card number

Note that mobile phone IMEI number also follows the Luhn algorithm, go ahead and test it for your credit card numbers. :)

About Pankaj

If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Google Plus, Facebook or Twitter. I would love to hear your thoughts and opinions on my articles directly.

Recently I started creating video tutorials too, so do check out my videos on Youtube.

Some of the video tutorials on Udemy I would recommend are;

Join our email newsletter to get promo codes for almost 90% discount on these courses, and yes you will get a lot of free eBooks too, absolutely Free.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to JournalDev Newsletter
Get the FREE access to JournalDev Newsletter and Free PDF eBooks
*No Spam Guaranteed. By entering your email address, you agree also subscribing to our newsletter.
Oops! - Something went wrong.
Close