forked from CodingWithAmit-07/Hacktoberfest-22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Credit Card Validator
66 lines (49 loc) · 1.81 KB
/
Credit Card Validator
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
bool isNumberString(const string& s) {
int len = s.length();
for (int i = 0; i < len; i++) {
if (s[i] < '0' || s[i] > '9')
return false;
}
return true;
}
int main() {
string ccNumber;
cout << "This program uses the Luhn Algorigthm to validate a CC number." << endl;
cout << "You can enter 'exit' anytime to quit." << endl;
while (true) {
cout << "Please enter a CC number to validate: ";
cin >> ccNumber;
if (ccNumber == "exit")
break;
else if (!isNumberString(ccNumber)) {
cout << "Bad input! ";
continue;
}
int len = ccNumber.length();
int doubleEvenSum = 0;
// Step 1 is to double every second digit, starting from the right. If it
// results in a two digit number, add both the digits to obtain a single
// digit number. Finally, sum all the answers to obtain 'doubleEvenSum'.
for (int i = len - 2; i >= 0; i = i - 2) {
int dbl = ((ccNumber[i] - 48) * 2);
if (dbl > 9) {
dbl = (dbl / 10) + (dbl % 10);
}
doubleEvenSum += dbl;
}
// Step 2 is to add every odd placed digit from the right to the value
// 'doubleEvenSum'.
for (int i = len - 1; i >= 0; i = i - 2) {
doubleEvenSum += (ccNumber[i] - 48);
}
// Step 3 is to check if the final 'doubleEvenSum' is a multiple of 10.
// If yes, it is a valid CC number. Otherwise, not.
cout << (doubleEvenSum % 10 == 0 ? "Valid!" : "Invalid!") << endl;
continue;
}
return 0;
}