Java Program for credit card number validation

Given a long number containing digits of a credit card number; the task is to find whether the credit card number is valid or not with a program.

For checking a credit card is valid or not, the following are the validations we have to be sure for declaring the result.

A credit card’s number must have 13 to 16 digits, it must start with the following digits.

• All the visa cards start from 4
• All the master cards start from 5
• 37 is the starting for American express cards
• All the discover cards start from 6

Steps to check whether the credit card is valid or not −

Step 1 − Starting from the right to left we have to double each the digit, if the result of doubling the number is one digit then leave it as it is, else add up the two digit to get an one digit number. (like 22 = 2+2= 4)

Step 2 − Starting from right to left of the card number add all the digits at the odd places.

Step 3 − Add all the single digit number obtained from Step 1.

Step 4 − Add up the results from Step 2 and Step 3.

Step 5 − If the result is divisible by 10 then the card number is valid else the number is not valid.

Example

Input: n = 4440967484181607
Output: 4440967484181607 is valid

Input: n = 379354508162306
Output: 379354508162306 is valid

Approach we are using to solve the problem

We will be using Luhn check or the mod 10 check, for the digit 4440967484181607.

Algorithm

Start
Step1-> In function void main(String[] args)
Declare and initialize cnumber = 4440967484181607L
Call function validitychk
Print the result
Step 2-> In function boolean validitychk(long cnumber)
Return thesize(cnumber) >= 13 && thesize(cnumber) <= 16) && (prefixmatch(cnumber, 4) ||
prefixmatch(cnumber, 5) || prefixmatch(cnumber, 37) || prefixmatch(cnumber, 6)) && ((sumdoubleeven(cnumber) +
sumodd(cnumber)) % 10 == 0
Step 3-> In function int sumdoubleeven(long cnumber)
Declare and set sum = 0
Declare and set num = cnumber + ""
Loop For i = thesize(cnumber) – 2 and i >= 0 and i -= 2
Set sum = sum + getDigit(Integer.parseInt(num.charAt(i) + "") * 2)
Return sum
Step 4-> In function int getDigit(int cnumber)
if cnumber < 9 then,
Return cnumber
Return cnumber / 10 + cnumber % 10
Step 5-> In function int sumodd(long cnumber)
Set sum = 0
Set num = cnumber + ""
Loop For i = thesize(cnumber) – 1 and i >= 0 and i -= 2
Set sum = sum + Integer.parseInt(num.charAt(i) + "")
Return sum
Step 6-> In function boolean prefixmatch(long cnumber, int d)
Return getprefx(cnumber, thesize(d)) == d
Step 7-> In function int thesize(long d)
Set num = d + ""
Return num.length()
Step8-> In function long getprefx(long cnumber, int k)
If thesize(cnumber) > k then,
Set num = cnumber + ""
Return Long.parseLong(num.substring(0, k))
Return cnumber
Stop

Example

Live Demo

import java.util.Scanner;
public class Main {
// Main Method
public static void main(String[] args) {
long cnumber = 4440967484181607L;
System.out.println(cnumber + " is " + (validitychk(cnumber) ? "valid" : "invalid"));
}
// Return true if the card number is valid
public static boolean validitychk(long cnumber) {
return (thesize(cnumber) >= 13 && thesize(cnumber) <= 16) && (prefixmatch(cnumber, 4)
|| prefixmatch(cnumber, 5) || prefixmatch(cnumber, 37) || prefixmatch(cnumber, 6))
&& ((sumdoubleeven(cnumber) + sumodd(cnumber)) % 10 == 0);
}
// Get the result from Step 2
public static int sumdoubleeven(long cnumber) {
int sum = 0;
String num = cnumber + "";
for (int i = thesize(cnumber) - 2; i >= 0; i -= 2)
sum += getDigit(Integer.parseInt(num.charAt(i) + "") * 2);
return sum;
}
// Return this cnumber if it is a single digit, otherwise,
// return the sum of the two digits
public static int getDigit(int cnumber) {
if (cnumber < 9)
return cnumber;
return cnumber / 10 + cnumber % 10;
}
// Return sum of odd-place digits in cnumber
public static int sumodd(long cnumber) {
int sum = 0;
String num = cnumber + "";
for (int i = thesize(cnumber) - 1; i >= 0; i -= 2)
sum += Integer.parseInt(num.charAt(i) + "");
return sum;
}
// Return true if the digit d is a prefix for cnumber
public static boolean prefixmatch(long cnumber, int d) {
return getprefx(cnumber, thesize(d)) == d;
}
// Return the number of digits in d
public static int thesize(long d) {
String num = d + "";
return num.length();
}
// Return the first k number of digits from
// number. If the number of digits in number
// is less than k, return number.
public static long getprefx(long cnumber, int k) {
if (thesize(cnumber) > k) {
String num = cnumber + "";
return Long.parseLong(num.substring(0, k));
}
return cnumber;
}
}

Output

4440967484181607 is valid