# Credit Card Validation in Java

Payment by credit card is one of the most widely used payment methods. During online transactions, we will enter the credit card number for payment. But have you ever wondered how the software checks whether our credit number is valid?. This article will show you how to check whether a credit card number is valid in Java.

The credit card number must be 13-16 digits and start with the following digits.
All American Express cards start with 37

## Implementation of credit card validator

1. Check whether number of digits in given credit card number is between 13 to 16.

2. Check the prefic of the credit card number.

3. Starting from right to left, we need to double each number. If the result of doubling the number is a single digit, keep it as it is, otherwise add the two digits to get a single digit.

4. Starting from right to left of the card number, add all the numbers in the odd positions.

5. Add all the single-digit numbers obtained from Step 3

6. Add the results of step 4 and step 5.

7. If the result is divisible by 10, the card number is valid; otherwise, the number is invalid.

```package PraticeProblems;

import java.util.*;

public class creditCardValidation {
public static boolean validation(long credit_number) {
return (sizecheck(credit_number)>= 13 && sizecheck(credit_number) <= 16 &&
(prefixcheck(credit_number, 4) || prefixcheck(credit_number, 5) ||
prefixcheck(credit_number, 37) || prefixcheck(credit_number, 6)) &&
(sumOfEven(credit_number)+sumOfOdd(credit_number)) % 10 == 0);
}
public static int sizecheck(long c_num) {
String num = c_num+"";
return num.length();
}
public static boolean prefixcheck(long c_num, int d) {
return getprefix(c_num, sizecheck(d)) == d;
}
public static long getprefix(long c_num, int k) {
if(sizecheck(c_num)>k) {
String num = c_num + "";
return Long.parseLong(num.substring(0, k));
}
return c_num;
}
public static int sumOfOdd(long c_num) {
int sum = 0;
String num = c_num + "";
for(int i = sizecheck(c_num)-1; i >= 0; i -= 2) {
sum += Integer.parseInt(num.charAt(i)+"");
}
return sum;
}
public static int sumOfEven(long c_num) {
int sum = 0;
String num = c_num + "";
for(int i = sizecheck(c_num)-1; i >= 0; i -= 2) {
sum += getDigit(Integer.parseInt(num.charAt(i)+""));
}
return sum;
}
public static int getDigit(int num) {
if(num<9)
return num;
return num/10 + num%10;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long credit_number;
System.out.print("Enter credit card number: ");
credit_number = sc.nextLong();
sc.close();
System.out.println(credit_number + " is "+ (validation(credit_number) ? "valid":"invalid"));
if(prefixcheck(credit_number, 4)){
System.out.print("The given credit card is visa card");
}else if(prefixcheck(credit_number, 5)){
System.out.print("The given credit card is master card");
}else if(prefixcheck(credit_number, 6)){
System.out.print("The given credit card is discover card");
}else if(prefixcheck(credit_number, 37)){
System.out.print("The given credit card is american express card");
}else {
System.out.print("The given credit card is does not belong given any given class");
}
}
}
```

Output

`Enter credit card number: 44409674841816074440967484181607 is invalidThe given credit card belongs to visa card`

Also, refer