# credit.c

Aug 23rd, 2019
538
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. // This program will check if the credit or debit card is valid using the Luhn's algorithm. It can check the American Express, Master Card and VISA cards
2.
3. #include <stdio.h>
4. #include <cs50.h>
5.
6. // Functions needed to calculate part of the Luhn's algorithm
7. int odd(long number);
8. int even(long number);
9.
10. int main(void)
11. {
12.     // Declaration of variable "number" (for input from user) and card to check the card type
13.     long number = 0;
14.     int card = 0;
15.     // Loop to get valid input from the user
16.     do
17.     {
18.         // Data type "long" so there will be enough memory for a valid card number
19.         number = get_long("Number: ");
20.         // Checking the card type and storing it to variable "card", if wrong type, invalid number or string, prompting the number again
21.         // 0 = INVALID
22.         // 1 and 2 = AMEX (15 digits)
23.         // 3 = MASTERCARD (16 digits)
24.         // 4 and 5 = VISA (13 and 16 digits)
25.         if (number > 339999999999999.9 && number < 349999999999999.9)
26.         {
27.             // AMEX 1
28.             card = 1;
29.         }
30.         else if (number > 369999999999999.9 && number < 379999999999999.9)
31.         {
32.             // AMEX 2
33.             card = 2;
34.         }
35.         else if (number > 5099999999999999.9 && number < 5599999999999999.9)
36.         {
37.             // MASTERCARD
38.             card = 3;
39.         }
40.         else if (number > 3999999999999.9 && number < 4999999999999.9)
41.         {
42.             // VISA 1
43.             card = 4;
44.         }
45.         else if (number > 3999999999999999.9 && number < 4999999999999999.9)
46.         {
47.             // VISA 2
48.             card = 5;
49.         }
50.         else
51.         {
52.             // INVALID
53.             printf("INVALID\n");
54.             exit(0);
55.         }
56.     }
57.     while (card != 1 && card != 2 && card != 3 && card != 4 && card != 5);
58.     // Checking validity of the card
59.     int a = even(number);
60.     int b = odd(number);
61.     int check = ((a + b) % 10);
62.     if (check == 0)
63.     {
64.         if (card == 1 || card == 2)
65.         {
66.             printf("AMEX\n");
67.         }
68.         else if (card == 3)
69.         {
70.             printf("MASTERCARD\n");
71.         }
72.         else if (card == 4 || card == 5)
73.         {
74.             printf("VISA\n");
75.         }
76.     }
77.     else
78.     {
79.         printf("INVALID\n");
80.     }
81. }
82.
83. // Function for calculating the odd numbers total
84. int odd(long number)
85. {
86.     int n1 = 0;
87.     int n2 = 0;
88.     for (long i = 0, j = 1000, k = 100; i < 7; i++)
89.     {
90.         n1 = ((number % j) / k);
91.         n2 = n2 + n1;
92.         j = j * 100;
93.         k = k * 100;
94.     }
95.     int n0 = (number % 10);
96.     int odd = n2 + n0;
97.     return odd;
98. }
99.
100. // Function for calculating the even numbers total
101. int even(long number)
102. {
103.     int n1 = 0;
104.     int even = 0;
105.     for (long i = 0, j = 100, k = 10; i < 8; i++)
106.     {
107.         n1 = (((number % j) / k) * 2);
108.         if (n1 > 9)
109.         {
110.             n1 = (n1 - 9);
111.         }
112.         even = (even + n1);
113.         j = j * 100;
114.         k = k * 100;
115.     }
116.     return even;
117. }
RAW Paste Data