-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuhnsChecksum.java
More file actions
43 lines (39 loc) · 1.33 KB
/
Copy pathLuhnsChecksum.java
File metadata and controls
43 lines (39 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// check if a credit card number is valid using Luhn's algorithm
// look out for where it starts
import java.util.Scanner;
public class LuhnsChecksum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
if (checksumAlgorithm(sc.next()) == 0)
System.out.println("PASS");
else
System.out.println("FAIL");
}
}
public static int checksumAlgorithm(String digits) {
int sum = 0;
int digit;
// read carefully where to start from
for (int i = digits.length() - 1; i >= 0; i -= 1) {
// length is odd, i must be even to manipulate
// length is even, i must be odd
if (digits.length() % 2 == 1 && i % 2 == 0 || digits.length() % 2 == 0 && i % 2 == 1) {
sum += Integer.parseInt("" + digits.charAt(i));
continue;
}
// now we're at every second digit
digit = Integer.parseInt("" + digits.charAt(i));
digit *= 2;
if (digit >= 10) {
sum += digit % 10 + digit / 10;
} else {
sum += digit;
}
}
if (sum % 10 == 0)
return 0;
return -1;
}
}