-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit.c
135 lines (115 loc) · 2.11 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//to check wether the bank card is valid or not.
#include <stdio.h>
#include <cs50.h>
int digit(long cardno);
int main(void)
{
//
long card;
int s;
card = get_long_long("Number: ");
s = digit(card);
//
int no[16];
long ten = 1;
for (int i = 0; i < 16; i++)
{
long q = card % (ten * 10);
long w = card % ten;
no[i] = (q - w) / ten;
ten = ten * 10;
}
int visa = no[15];
int master = (no[15] * 10) + no[14];
int amex = (no[14] * 10) + no[13];
//
int no1[8];
int h = 0;
for (int v = 1; v < 16; v = v + 2)
{
no[v] = no[v] * 2;
no1[h] = ((no[v] % 100) - (no[v] % 10)) / 10;
no[v] = no[v] % 10;
h++;
}
//
int a = 0;
for (int o = 0; o < 16; o = o + 2)
{
a = a + no[o];
}
//
int b = 0;
for (int x = 1; x < 16; x = x + 2)
{
b = b + no[x];
}
//
int e = 0;
for (int y = 0; y < 8; y++)
{
e = e + no1[y];
}
int c = a + b + e;
int d = c % 10;
//
if (d == 0)
{
if (s == 15)
{
if (amex == 34 || amex == 37)
{
printf("AMEX\n");
}
else
{
printf("INVALID\n");
}
}
else if (s == 13)
{
printf("VISA\n");
}
else if (s == 16)
{
if (visa == 4)
{
printf("VISA\n");
}
else if (master == 51 || master == 52 || master == 53 || master == 54 || master == 55)
{
printf("MASTERCARD\n");
}
else
{
printf("INVALID\n");
}
}
else
{
printf("INVALID\n");
}
}
else
{
printf("INVALID\n");
}
}
//
int digit(long cardno)
{
//
long d = 1, f;
int e = 0;
//
do
{
f = cardno % d;
d = d * 10;
e++;
}
while (cardno != f);
//
e--;
return e;
}