Advertisement
ssoni

credit.c

Jan 13th, 2022
1,967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cs50.h>
  4.  
  5. int main(void)
  6. {
  7.     //enter CC
  8.     long cc=0;
  9.     cc = get_long("Number: ");
  10.  
  11.     //make copies variables of CC (4)
  12.     //When we process digits, we delete them
  13.     long cc1,cc2,cc3,cc4;
  14.     cc1 = cc;
  15.     cc2 = cc;
  16.     cc3 = cc;
  17.     cc4 = cc;
  18.  
  19.     int d=0;
  20.     int a=0,b=0;
  21.     int sum=0;
  22.     int total=0;
  23.  
  24.     //Add alternate digits (pass 1)
  25.     //32452345 8345
  26.     cc1 = cc1 / 10;     //ignore last digit
  27.  
  28.     while (cc1>0)
  29.     {
  30.         d = cc1 % 10;       //get last digit
  31.         d = d * 2;          //double it
  32.         b = d % 10;         //get 2nd digit
  33.         a = d / 10;         //get 1st digit
  34.         sum = a + b;        //sum the 2 digits
  35.         total = total + sum;    //add sum to total
  36.         cc1 = cc1 / 100;        //chop off 2 digits
  37.     }
  38.  
  39.     //Add other unused digits (pass 2)
  40.     //8345
  41.     while (cc2>0)
  42.     {
  43.        d = cc2 % 10;
  44.        total = total + d;
  45.        cc2 = cc2 / 100;
  46.     }
  47.     //printf("%i", total);
  48.  
  49.     //Valid if total ends with a zero
  50.     //mod total by 10
  51.     if (total % 10 != 0)
  52.     {
  53.         printf("INVALID\n");
  54.         exit(0);
  55.     }
  56.  
  57.     int len=0;
  58.  
  59.     //Calc length of CC (digits)
  60.     while (cc3>0)
  61.     {
  62.         cc3 = cc3 / 10;         //chop off last digit
  63.         len++;
  64.     }
  65.     //printf("length=%i\n", len);
  66.  
  67.     //Calc 2 digit prefix
  68.     int prefix=0;
  69.  
  70.     //Keep chopping until there are only 2 digits left
  71.     while (cc4>99)
  72.     {
  73.         cc4 = cc4 / 10;         //chop off last digit
  74.     }
  75.     prefix = cc4;
  76.  
  77.     //Check all the stuff (length, prefix, valid)
  78.     //AMEX 15 digits, prefix 34/37
  79.     //MC 16 digits, prefix 51-55
  80.     //Visa 13/16 digits, prefix 40-49
  81.     if (len == 15 && (prefix == 34 || prefix == 37))
  82.     {
  83.         printf("AMEX\n");
  84.     } else if (len == 16 && (prefix >= 51 && prefix <= 55))
  85.     {
  86.         printf("MASTERCARD\n");
  87.     } else if ((len==13 ||len==16) && (prefix >= 40 && prefix <= 49))
  88.     {
  89.         printf("VISA\n");
  90.     } else
  91.     {
  92.         printf("INVALID\n");
  93.     }
  94.  
  95. }
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement