Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7c23ff2

Browse files
Aseem JainAseem Jain
Aseem Jain
authored and
Aseem Jain
committed
credit card validator
1 parent f23f66f commit 7c23ff2

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package me.premaseem.practiceRepeatation;
2+
3+
/*
4+
5+
Credit Card Validator
6+
Programming challenge description:
7+
Credit card numbers can be validated using an industry-standard algorithm called the Luhn Checksum:
8+
9+
From the rightmost digit (the check digit), double the value of every second digit. The check digit is not doubled; the first digit doubled is immediately to the left of the check digit. If the result of this doubling operation is greater than 9 (e.g., 8 ×ばつ 2 = 16), then subtract 9 from the product (e.g., 16: 16 − 9 = 7, 18: 18 − 9 = 9).
10+
Take the sum of all the digits from step 1, both doubled and natural.
11+
If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; otherwise it is not valid.
12+
The sum of all the digits in the third row is 67+x. If x = 3, then the modulo of 10 = 0, which means the credit card number is valid.
13+
14+
Write a program that will validate credit card numbers using this algorithm and indicate the result.
15+
16+
Input:
17+
A single line of input containing a potential credit card number
18+
19+
Output:
20+
True if the credit card number is valid
21+
False if the credit card number is not valid
22+
23+
Test Data:
24+
6011566926687256 - True
25+
370642420667124 - True
26+
5417438903761250 - True
27+
4518377421351212 - False
28+
29+
*/
30+
31+
import java.io.BufferedReader;
32+
import java.io.BufferedReader;
33+
import java.io.IOException;
34+
import java.io.InputStreamReader;
35+
import java.nio.charset.StandardCharsets;
36+
import java.util.ArrayList;
37+
import java.util.List;
38+
39+
public class Credit_card_validator {
40+
41+
public static void main(String[] args) throws IOException {
42+
InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
43+
BufferedReader in = new BufferedReader(reader);
44+
String line;
45+
List<String> arr = new ArrayList();
46+
while ((line = in.readLine()) != null) {
47+
// System.out.println(line);
48+
char[] ca = line.toCharArray();
49+
System.out.println(ca);
50+
int os = 0;
51+
for (int i = ca.length-1; i>=0;i=i-2){
52+
os += Integer.parseInt(""+ca[i]);
53+
}
54+
int es = 0;
55+
for (int i = ca.length-2; i>=0; i=i-2 ){
56+
Integer d = Integer.parseInt(""+ca[i]) *2;
57+
d = d <10 ? d: d - 9;
58+
os += d;
59+
}
60+
61+
System.out.println((os + es) % 10 == 0);
62+
63+
}
64+
}
65+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /