-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredit.c
More file actions
121 lines (102 loc) · 3.39 KB
/
credit.c
File metadata and controls
121 lines (102 loc) · 3.39 KB
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
// CHALLENG 4
// This is CS50
// Objective: Prompt user for valid credit card number and return card type (VISA, AMEX, MASTERCARD) if number matches formatting and passes Luhn's Algorithm
#include <cs50.h>
#include <stdio.h>
int main(void)
{
long card_number;
string check;
int checksum(long card_number);
int first_digit;
int get_first_digit(long card_number);
int first_two_digits;
int get_first_two_digits(long card_number);
string card_type;
/* Prompt user for card number with no hyphens;
Use get_long for up to 16 digits */
do
{
card_number = get_long("Please enter your card number (without hyphens): ");
}
while (card_number < 0);
/* Calulate checksum
Multiply every other digit by 2, starting with second to last digit;
Add those products' digits together;
Add the sum to the sum of the digits that weren't multiplied by 2. */
checksum(card_number);
if (checksum(card_number) == 0)
{
check = "true";
}
else
check = "false";
/* Determine first digit to recognize a Visa card */
/* Determine the first two digits to recognize MAstercard and Amex */
first_digit = get_first_digit(card_number);
first_two_digits = get_first_two_digits(card_number);
/* Determine the total digits in card_number */
int digits = 0;
while (card_number > 0)
{
card_number /= 10;
digits++;
}
/* Determine card type */
/* According to the instructions:
Mastercard has 16 digits and begins with 51, 52, 53, 54, 55;
Visa has 13 and 16 digits and begins with 4;
Amex has 15 digits and begins with 34, 37. */
/* Cards that don't match the Mastercard, Visa, Amex format
are considered invalid in teh context of this program.*/
if ((checksum(card_number) == 0) && (first_two_digits == 51 || first_two_digits == 52 || first_two_digits == 54
|| first_two_digits == 54|| first_two_digits == 55) && (digits == 16))
{
card_type = "MASTERCARD";
}
else if ((checksum(card_number) == 0) && (first_digit == 4) && (digits == 16 || digits == 13))
{
card_type = "VISA";
}
else if ((checksum(card_number) == 0) && (first_two_digits == 34 || first_two_digits == 37) && (digits == 15))
{
card_type = "AMEX";
}
else
{
card_type = "INVALID";
}
/* Printing out more data for fun */
// printf("Checksum passed:\n %s\n\n Number of digits on card:\n %i\n\n First digit on card:\n %i\n\n First two digts on card:\n %i\n\n Card type:\n %s\n",
// check, digits, first_digit, first_two_digits,
// card_type);
printf("card_number: %li\n\n product sum: %i\n\n sum: %i\n\n", card_number, product_sum, sum);
}
int get_first_digit(long card_number)
{
while (card_number >= 10)
{
card_number /= 10;
}
return card_number;
}
int get_first_two_digits(long card_number)
{
while (card_number >= 100)
{
card_number /= 10;
}
return card_number;
}
int checksum(long card_number)
{
int product_sum = 0;
int sum = 0;
while (card_number > 0)
{
sum += (card_number % 10);
card_number /= 10; //Divide by 10 to remove last value
product_sum += (card_number % 10) * 2; //Modulo by 10 to remove all values except last (second to last)
}
return (sum + product_sum) % 10;
}