In: Computer Science
The following algorithm is widely used for checking whether a credit or debit card number has been entered correctly on a website. It doesn't guarantee that the credit card number belongs to a valid card, but it rules out numbers which are definitely not valid. Here are the steps:
Example
Here is a worked example using the long number "4916592478445662".
longNumber index | longNumber value at index | values at even indexes multiplied by 2 | Adjusted to a single digit by subtracting 9 |
---|---|---|---|
0 | 4 | 8 | 8 |
1 | 9 | … | 9 |
2 | 1 | 2 | 2 |
3 | 6 | … | 6 |
4 | 5 | 10 | 1 |
5 | 9 | … | 9 |
6 | 2 | 4 | 4 |
7 | 4 | … | 4 |
8 | 7 | 14 | 5 |
9 | 8 | … | 8 |
10 | 4 | 8 | 8 |
11 | 4 | … | 4 |
12 | 5 | 10 | 1 |
13 | 6 | … | 6 |
14 | 6 | 12 | 3 |
S = 8 + 9 + 2 + 6 + 1 + 9 + 4 + 4 + 5 + 8 + 8 + 4 + 1 + 6 + 3 = 78
a- Write a public method called isCorrectLength() that takes no arguments and returns the boolean value true if the length of longNumber is 16 and false otherwise.
b - Write a public method firstFifteen() that returns a String consisting of the first fifteen characters in the longNumber.
c) - write a public method calculateCheckNumber() to find S and then use the expression S/10 * 10 + 10 – S to find C. The method should first create a string omitting the last digit of the longNumber and then find S by iterating through this string. (You may choose to do a separate iteration for the odd and even indexes, or you could do both in a single loop.)
Here's the Java Code for the same:
package javaProject;
import java.util.Stack;
public class Luhn {
public static boolean isCorrectLength(long cardNo) {
int len = 0;
// loop to find the length
while (cardNo > 0) {
len++;
cardNo /= 10;
}
// if valid length
if (len == 16)
return true;
// else
return false;
}
public static String firstFifteen(long cardNo) {
// discarding the last digit
cardNo /= 10;
// the first fifteen digits string to be returned
String firstFifteenDigits = new String("" + cardNo);
return firstFifteenDigits;
}
public static int calculateCheckNumber(long cardNo) {
// get the first fifteen digits string
String firstFifteen = firstFifteen(cardNo);
int sum = 0, dig;
// iterate over the string and calculate the sum
for (int i = 0; i < firstFifteen.length(); ++i) {
// if current digit position is even then double the digit and if it goes above 10 then subtract 9 from it & finally add to sum
if (i % 2 == 0) {
dig = 2 * (firstFifteen.charAt(i) % 48);
if (dig > 9)
dig -= 9;
sum += dig;
}
// if position is odd simply add the current rightmost digit to the sum
else
sum += firstFifteen.charAt(i) % 48;
}
// return the required value
return ((sum / 10) * 10 + 10 - sum);
}
public static void main(String[] args) {
long cardNo = 4916592478445662L;
// check for correct length
if (isCorrectLength(cardNo)) {
// check if the last digit is equal to the check number or not and print a message accordingly
if (calculateCheckNumber(cardNo) == cardNo % 10)
System.out.println("Valid Card number!");
else System.out.println("Invalid Card number");
}
else System.out.println("Incorrect length of card number");
}
}
Added comments in the code for explanation of each step, do refer them :). For any doubt, feel free to reach back.
Sample Output for the card number used in the code:
Hope it helps, consider hitting a like if it did :)
Cheers!