-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit.c
106 lines (99 loc) · 2.26 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
#include <cs50.h>
#include <stdio.h>
int specific_digit(long n, long len,long place);
int check_len(long n);
int luhn_algo(long n);
int main(void)
{
long credit_no;
credit_no=get_long("Number: ");
if (luhn_algo(credit_no)%10==0)
{
if ((check_len(credit_no)==15)&&(specific_digit(credit_no,check_len(credit_no),1)==3)&&((specific_digit(credit_no,check_len(credit_no),2)==4)||(specific_digit(credit_no,check_len(credit_no),2)==7)))
{
printf("AMEX\n");
}
else if (((check_len(credit_no)==13)||(check_len(credit_no)==16))&&(specific_digit(credit_no,check_len(credit_no),1)==4))
{
printf("VISA\n");
}
else if ((check_len(credit_no)==16)&&(specific_digit(credit_no,check_len(credit_no),1)==5)&&((specific_digit(credit_no,check_len(credit_no),2)>0)&&(specific_digit(credit_no,check_len(credit_no),2)<6)))
{
printf("MASTERCARD\n");
}
else
{
printf("INVALID\n");
}
}
else
{
printf("INVALID\n");
}
}
int check_len(long n)
{
int counter=0;
do
{
n/=10;
counter+=1;
}
while(n!=0);
return counter;
}
int digits(long n, int len)
{
long digit;
int sum=0;
for (int i=0;i<len;i++)
{
long multiplier=1;
for(int j=0;j<len-i-1;j++)
{
multiplier*=10;
}
digit=n/multiplier%10;
sum+=digit;
}
return sum;
}
int luhn_algo(long n)
{
int check_len(long n);
int digits(long n, int len);
long digit;
int sum=0;
int temp;
for (int i=0;i<check_len(n);i++)
{
long multiplier=1;
for(int j=0;j<check_len(n)-i-1;j++)
{
multiplier*=10;
}
digit=n/multiplier%10;
if (((check_len(n)%2==0)&&(i%2==0))||((check_len(n)%2==1)&&(i%2==1)))
{
temp=(int)digit*2;
sum+=digits(temp, check_len(temp));
}
else
{
sum+=(int)digit;
}
}
return sum;
}
int specific_digit(long n, long len,long place)
{
int check_len(long n);
int digit;
long multiplier=1;
for(int j=0;j<len-place;j++)
{
multiplier*=10;
}
digit=n/multiplier%10;
return digit;
}