-
Notifications
You must be signed in to change notification settings - Fork 2
/
Credit.c
95 lines (73 loc) · 1.81 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
#include <stdio.h>
#include <cs50.h>
int main(void)
{
long CCNumber;
//To get the input from the user for Credit Card Number:
do
{
CCNumber = get_long("Input the Credit Card Number: ");
}
while (CCNumber <= 0);
//1st case where we take the sum from last digit:
long CNumber = CCNumber;
int sum = 0;
while (CNumber > 0)
{
int LastDigit = CNumber % 10;
sum = sum + LastDigit;
//To go to the next number after one place:
CNumber = CNumber / 100;
}
//2nd case where we take the numbers from last second digit:
long CNum;
CNum = CCNumber / 10;
while (CNum > 0)
{
int lastDigit = CNum % 10;
//Multiplication by 2 to each number:
int TwiceTimes = lastDigit * 2;
sum = sum + (TwiceTimes % 10) + (TwiceTimes / 10);
CNum = CNum / 100;
}
//To find the Length of Number:
long CNo = CCNumber;
int count = 0;
while (CNo > 0)
{
CNo = CNo / 10;
count++;
}
//To divide the number:
long divisor = 10;
for (int i = 0; i < count - 2; i++)
{
divisor = divisor * 10;
}
//Final Check for Card Type:
int FirstDigit = CCNumber / divisor;
int FirstTwoDigits = CCNumber / (divisor / 10);
if ((sum % 10) == 0)
{
if (FirstDigit == 4 && (count == 13 || count == 16))
{
printf("VISA\n");
}
else if ((FirstTwoDigits == 34 || FirstTwoDigits == 37) && (count == 15))
{
printf("AMEX\n");
}
else if ((FirstTwoDigits > 50 && FirstTwoDigits < 56) && (count == 16))
{
printf("MASTERCARD\n");
}
else
{
printf("INVALID\n");
}
}
else
{
printf("INVALID\n");
}
}