-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcredit.c
67 lines (62 loc) · 1.38 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
#include <stdio.h>
#include <cs50.h>
int main()
{
long credit_card_number = get_long("Number: ");
long cc = credit_card_number;
int sum = 0;
int digits = 0;
bool is_other_digit = false;
while (credit_card_number != 0)
{
int number = credit_card_number % 10;
if (is_other_digit)
{
number *= 2;
if (number > 9)
{
int first_digit = number % 10;
int second_digit = number / 10;
sum += (first_digit + second_digit);
}
else
{
sum += number;
}
}
else
{
sum += number;
}
digits++;
is_other_digit = !is_other_digit;
credit_card_number = credit_card_number / 10;
}
if (sum % 10 == 0)
{
while (cc > 100)
{
cc = cc / 10;
}
if (cc >= 51 && cc <= 55 && digits == 16)
{
printf("MASTERCARD\n");
}
else if ((cc == 34 || cc == 37) && digits == 15)
{
printf("AMEX\n");
}
else if ((cc >= 40 && cc <= 49) && (digits == 13 || digits == 16))
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
else
{
printf("INVALID\n");
}
}