forked from maciejczyzewski/libchaos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluhn.c
46 lines (37 loc) · 885 Bytes
/
luhn.c
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
#include <stdio.h>
#include <math.h>
int getNumLength (long long);
int sumOfDigits(int);
int getCardType(int);
int main() {
long long card_number_input = "enter number here";
short l = getNumLength(card_number_input);
short card_number[l];
int checksum=0;
for (int i=0;i<l;i++) {
card_number[i]=card_number_input % 10;
card_number_input = card_number_input /10;
}
for (int i=0;i<l;i++) {
if (i%2==1) {
checksum+=sumOfDigits(card_number[i]*2);
}else{
checksum+=card_number[i];
}
}
if (checksum%10==0) printf("VALID\n");
return 0;
}
int sumOfDigits (int number) {
int sum = 0;
while (number>0) {
sum+=number%10;
number /= 10;
}
return sum;
}
int getNumLength (long long number) {
int l = 1;
l = floor(log10(number))+1;
return l;
}