This repository has been archived by the owner on Oct 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
credit.c
101 lines (89 loc) · 2.64 KB
/
credit.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <cs50.h>
int main(void)
{
printf("Number: ");
long card_number_input = get_long_long();
int total_digits = 0;
long card_number_temp = card_number_input;
/* Get card number length */
while (card_number_temp != 0) {
card_number_temp /= 10;
total_digits++;
}
/* Check card number length */
if (total_digits < 13 || total_digits > 16) {
printf("INVALID\n");
return 0;
}
int card_number[total_digits];
card_number_temp = card_number_input;
int temp = total_digits;
/* Create an array with card numbers */
while (card_number_temp != 0) {
temp--;
card_number[temp] = (int) (card_number_temp % 10);
card_number_temp /= 10;
}
/* Check first numbers for validity */
if ((card_number[0] != 4) &&
(card_number[0] != 3 && (card_number[1] != 4 || card_number[1] != 7)) &&
(card_number[0] != 5 && (card_number[1] != 1 || card_number[1] != 2 || card_number[1] != 3 ||
card_number[1] != 4 || card_number[1] != 5))) {
printf("INVALID\n");
return 0;
}
int odd_numbers = 0, even_numbers = 0, sum_of_numbers = 0;
/* Use Luhn’s algorithm to determine if card number is syntactically valid */
if(total_digits % 2 == 0) {
for (int i = 0; i < total_digits; ++i) {
if (i % 2 == 0) {
temp = card_number[i] * 2;
if(temp >= 10) {
even_numbers += temp % 10;
even_numbers += temp / 10;
}
else {
even_numbers += temp;
}
}
else {
odd_numbers += card_number[i];
}
}
}
else {
for (int i = 0; i < total_digits; ++i) {
if (i % 2 != 0) {
temp = card_number[i] * 2;
if(temp >= 10) {
even_numbers += temp % 10;
even_numbers += temp / 10;
}
else {
even_numbers += temp;
}
}
else {
odd_numbers += card_number[i];
}
}
}
sum_of_numbers = even_numbers + odd_numbers;
if (sum_of_numbers % 10 != 0) {
printf("INVALID\n");
}
else {
switch (card_number[0]) {
case 3:printf("AMEX\n");
break;
case 4:printf("VISA\n");
break;
case 5:printf("MASTERCARD\n");
break;
default:printf("INVALID\n");
break;
}
}
return 0;
}