Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This is program to check if credit card numbers are valid using Luhns algorithm and simple rules for the starting numbers and
- length of credit cards
- */
- #include <cs50.h>
- #include <stdio.h>
- long mypow(int base, int power);
- int countdigits(long number);
- int sumotherdigits(long c_card);
- int multandsumdigits(long c_card);
- bool luhncheck(int mutlandsumd, int sumofdigits);
- int cardtype(long c_card);
- int main(void)
- {
- long c_card = get_long("Number: ");
- int c_type = cardtype(c_card);
- string c_brand;
- //We first check the brand and only go forward if its not Invalid
- if (c_type == 1)
- {
- c_brand = "VISA";
- }
- else if (c_type == 2)
- {
- c_brand = "AMEX";
- }
- else if (c_type == 3)
- {
- c_brand = "MASTERCARD";
- }
- else
- {
- printf("INVALID\n");
- }
- //Here we do the Luhn formula and if it passes we print the card type
- if (c_type != 0)
- {
- int sumofdigits = sumotherdigits(c_card);
- int mutlandsumd = multandsumdigits(c_card);
- if (luhncheck(sumofdigits, mutlandsumd) == true)
- {
- printf("%s\n", c_brand);
- }
- else
- {
- printf("INVALID\n");
- }
- }
- }
- //This determines card type using simple rules
- int cardtype(long c_card)
- {
- int t_digits = countdigits(c_card);
- //We substract 2 from the total digits so we can get the first two digits
- int firstdigits = c_card / (mypow(10, (t_digits - 2)));
- if ((t_digits == 13 || t_digits == 16) && ((firstdigits / 10) == 4))
- {
- //return "VISA";
- return 1;
- }
- else if (t_digits == 15 && (firstdigits == 34 || firstdigits == 37))
- {
- //return "AMEX";
- return 2;
- }
- //I added the 22 rule since in the Paypal site some MC numbers start with 22
- else if (t_digits == 16 && ((firstdigits > 50 && firstdigits < 56) || firstdigits == 22))
- {
- //return "MASTERCARD";
- return 3;
- }
- else
- {
- //return "INVALID";
- return 0;
- }
- }
- //This checks using the Luhn formula
- bool luhncheck(int mutlandsumd, int sumofdigits)
- {
- //In essence the Luhn formula says that if the sum of the formula ends in zero its valid
- //This will check that
- int total = mutlandsumd + sumofdigits;
- if ((total % 10) == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //this will perform the sum of the digits not multiplied by 2
- int sumotherdigits(long c_card)
- {
- //First we get total digits and set a counter and put a temp card number variable to manipulate
- int t_digits = countdigits(c_card);
- int d_counter = t_digits;
- long cc_temp = c_card;
- int sumdigi = 0;
- /*
- We go through the cc number and each loop we chop two digits and get the last so that
- way we can go through each other digit. We also substract two form the counter to keep things consistent
- */
- while (d_counter > 0)
- {
- //printf("cctemp is: %li \n", cc_temp);
- long c_digit = cc_temp % 10;
- //printf("cdigit is: %li \n", c_digit);
- sumdigi += c_digit;
- //printf("sumdigi is: %i \n", sumdigi);
- cc_temp = cc_temp / 100;
- //printf("cctemp after chop is: %li \n", cc_temp);
- d_counter -= 2;
- }
- return sumdigi;
- }
- //This will perform the multiplication and sum of digits starting from the next to last
- int multandsumdigits(long c_card)
- {
- int t_digits = countdigits(c_card);
- //Since we start from the next to last we substract one number from total digits and chop the last number
- int d_counter = t_digits - 1;
- long cc_temp = c_card / 10;
- int sumdigi = 0;
- int multdigi;
- //This goes through the Credit card number as before, we just add an if to deal with the multiplication
- while (d_counter > 0)
- {
- //We get the current digit and then we multiply it by two
- long c_digit = cc_temp % 10;
- //printf("cdigit is: %li \n", c_digit);
- multdigi = c_digit * 2;
- int mcdigi = countdigits(multdigi);
- //This will check if a multiplication has more than 1 digit and sum the digits if so
- if (mcdigi > 1)
- {
- int tempdigi1 = multdigi % 10;
- int tempdigi2 = multdigi / 10;
- int ftempdigi = tempdigi1 + tempdigi2;
- sumdigi += ftempdigi;
- }
- else
- {
- sumdigi += multdigi;
- }
- //printf("sumdigi is: %i \n", sumdigi);
- //This will chop two numbers from the card to go to the next number according to Luhn (that is every other one)
- cc_temp = cc_temp / 100;
- //printf("cctemp after chop is: %li \n", cc_temp);
- d_counter -= 2;
- }
- return sumdigi;
- }
- //Simple function to count digits, basically a loop that will go around chopping a number until its zero
- //it counts each loop to get the digits
- int countdigits(long number)
- {
- int digits = 0;
- while (number > 0)
- {
- number = number / 10;
- digits++;
- }
- return digits;
- }
- //Simple exponation func
- long mypow(int base, int power)
- {
- long result = 1;
- if (power > 0)
- {
- for (int i = 0; i < power; i++)
- {
- result = result * base;
- }
- return result;
- }
- else if (power == 0)
- {
- return 1;
- }
- //It does not deal with negative power since its a long I just set zero as the answer instead of dealing
- //with data casting and stuff I wont need
- else
- {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment