Guest User

Untitled

a guest
Mar 19th, 2022
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.63 KB | None | 0 0
  1. /*
  2.  
  3.     This is program to check if credit card numbers are valid using Luhns algorithm and simple rules for the starting numbers and        
  4.     length of credit cards
  5.  
  6. */
  7.  
  8.  
  9. #include <cs50.h>
  10. #include <stdio.h>
  11.  
  12.  
  13. long mypow(int base, int power);
  14. int countdigits(long number);
  15. int sumotherdigits(long c_card);
  16. int multandsumdigits(long c_card);
  17. bool luhncheck(int mutlandsumd, int sumofdigits);
  18. int cardtype(long c_card);
  19.  
  20. int main(void)
  21. {
  22.     long c_card = get_long("Number: ");
  23.     int c_type = cardtype(c_card);
  24.     string c_brand;
  25.     //We first check the brand and only go forward if its not Invalid
  26.     if (c_type == 1)
  27.     {
  28.         c_brand = "VISA";
  29.     }
  30.     else if (c_type == 2)
  31.     {
  32.         c_brand = "AMEX";
  33.     }
  34.     else if (c_type == 3)
  35.     {
  36.         c_brand = "MASTERCARD";
  37.     }
  38.     else
  39.     {
  40.         printf("INVALID\n");
  41.     }
  42.  
  43.     //Here we do the Luhn formula and if it passes we print the card type
  44.     if (c_type != 0)
  45.     {
  46.         int sumofdigits = sumotherdigits(c_card);
  47.         int mutlandsumd = multandsumdigits(c_card);
  48.  
  49.         if (luhncheck(sumofdigits, mutlandsumd) == true)
  50.         {
  51.             printf("%s\n", c_brand);
  52.         }
  53.         else
  54.         {
  55.             printf("INVALID\n");
  56.         }
  57.     }
  58. }
  59.  
  60. //This determines card type using simple rules
  61. int cardtype(long c_card)
  62. {
  63.     int t_digits = countdigits(c_card);
  64.     //We substract 2 from the total digits so we can get the first two digits
  65.     int firstdigits = c_card / (mypow(10, (t_digits - 2)));
  66.  
  67.     if ((t_digits == 13 || t_digits == 16) && ((firstdigits / 10) == 4))
  68.     {
  69.         //return "VISA";
  70.         return 1;
  71.     }
  72.     else if (t_digits == 15 && (firstdigits == 34 || firstdigits == 37))
  73.     {
  74.         //return "AMEX";
  75.         return 2;
  76.     }
  77.     //I added the 22 rule since in the Paypal site some MC numbers start with 22
  78.     else if (t_digits == 16 && ((firstdigits > 50 && firstdigits < 56) || firstdigits == 22))
  79.     {
  80.         //return "MASTERCARD";
  81.         return 3;
  82.     }
  83.     else
  84.     {
  85.         //return "INVALID";
  86.         return 0;
  87.     }
  88. }
  89.  
  90. //This checks using the Luhn formula
  91. bool luhncheck(int mutlandsumd, int sumofdigits)
  92. {
  93.     //In essence the Luhn formula says that if the sum of the formula ends in zero its valid
  94.     //This will check that
  95.     int total = mutlandsumd + sumofdigits;
  96.     if ((total % 10) == 0)
  97.     {
  98.         return true;
  99.     }
  100.     else
  101.     {
  102.         return false;
  103.     }
  104. }
  105.  
  106. //this will perform the sum of the digits not multiplied by 2
  107. int sumotherdigits(long c_card)
  108. {
  109.     //First we get total digits and set a counter and put a temp card number                 variable to manipulate
  110.     int t_digits = countdigits(c_card);
  111.     int d_counter = t_digits;
  112.     long cc_temp = c_card;
  113.     int sumdigi = 0;
  114.  
  115.     /*
  116.     We go through the cc number and each loop we chop two digits and get the last so that
  117.     way we can go through each other digit. We also substract two form the counter to keep things consistent
  118.     */
  119.     while (d_counter > 0)
  120.     {
  121.         //printf("cctemp is: %li \n", cc_temp);
  122.         long c_digit = cc_temp % 10;
  123.         //printf("cdigit is: %li \n", c_digit);
  124.         sumdigi += c_digit;
  125.         //printf("sumdigi is: %i \n", sumdigi);
  126.         cc_temp = cc_temp / 100;
  127.         //printf("cctemp after chop is: %li \n", cc_temp);
  128.         d_counter -= 2;
  129.     }
  130.     return sumdigi;
  131. }
  132.  
  133. //This will perform the multiplication and sum of digits starting from the next to last
  134. int multandsumdigits(long c_card)
  135. {
  136.     int t_digits = countdigits(c_card);
  137.     //Since we start from the next to last we substract one number from total digits and chop the last number
  138.     int d_counter = t_digits - 1;
  139.     long cc_temp = c_card / 10;
  140.     int sumdigi = 0;
  141.     int multdigi;
  142.  
  143.     //This goes through the Credit card number as before, we just add an if to deal with the multiplication
  144.     while (d_counter > 0)
  145.     {
  146.         //We get the current digit and then we multiply it by two
  147.         long c_digit = cc_temp % 10;
  148.         //printf("cdigit is: %li \n", c_digit);
  149.         multdigi = c_digit * 2;
  150.         int mcdigi = countdigits(multdigi);
  151.         //This will check if a multiplication has more than 1 digit and sum the digits if so
  152.         if (mcdigi > 1)
  153.         {
  154.             int tempdigi1 = multdigi % 10;
  155.             int tempdigi2 = multdigi / 10;
  156.             int ftempdigi = tempdigi1 + tempdigi2;
  157.             sumdigi += ftempdigi;
  158.         }
  159.         else
  160.         {
  161.             sumdigi += multdigi;
  162.         }
  163.  
  164.         //printf("sumdigi is: %i \n", sumdigi);
  165.  
  166.         //This will chop two numbers from the card to go to the next number according to Luhn (that is every other one)
  167.         cc_temp = cc_temp / 100;
  168.  
  169.         //printf("cctemp after chop is: %li \n", cc_temp);
  170.  
  171.         d_counter -= 2;
  172.     }
  173.     return sumdigi;
  174. }
  175.  
  176.  
  177. //Simple function to count digits, basically a loop that will go around chopping a number until its zero
  178. //it counts each loop to get the digits
  179. int countdigits(long number)
  180. {
  181.     int digits = 0;
  182.     while (number > 0)
  183.     {
  184.         number = number / 10;
  185.         digits++;
  186.     }
  187.     return digits;
  188. }
  189.  
  190. //Simple exponation func
  191. long mypow(int base, int power)
  192. {
  193.     long result = 1;
  194.     if (power > 0)
  195.     {
  196.         for (int i = 0; i < power; i++)
  197.         {
  198.             result = result * base;
  199.         }
  200.         return result;
  201.     }
  202.     else if (power == 0)
  203.     {
  204.         return 1;
  205.     }
  206.     //It does not deal with negative power since its a long I just set zero as the answer instead of dealing
  207.     //with data casting and stuff I wont need
  208.     else
  209.     {
  210.         return 0;
  211.     }
  212. }
  213.  
Advertisement
Add Comment
Please, Sign In to add comment