Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <cs50.h>
- int main(void)
- {
- //enter CC
- long cc=0;
- cc = get_long("Number: ");
- //make copies variables of CC (4)
- //When we process digits, we delete them
- long cc1,cc2,cc3,cc4;
- cc1 = cc;
- cc2 = cc;
- cc3 = cc;
- cc4 = cc;
- int d=0;
- int a=0,b=0;
- int sum=0;
- int total=0;
- //Add alternate digits (pass 1)
- //32452345 8345
- cc1 = cc1 / 10; //ignore last digit
- while (cc1>0)
- {
- d = cc1 % 10; //get last digit
- d = d * 2; //double it
- b = d % 10; //get 2nd digit
- a = d / 10; //get 1st digit
- sum = a + b; //sum the 2 digits
- total = total + sum; //add sum to total
- cc1 = cc1 / 100; //chop off 2 digits
- }
- //Add other unused digits (pass 2)
- //8345
- while (cc2>0)
- {
- d = cc2 % 10;
- total = total + d;
- cc2 = cc2 / 100;
- }
- //printf("%i", total);
- //Valid if total ends with a zero
- //mod total by 10
- if (total % 10 != 0)
- {
- printf("INVALID\n");
- exit(0);
- }
- int len=0;
- //Calc length of CC (digits)
- while (cc3>0)
- {
- cc3 = cc3 / 10; //chop off last digit
- len++;
- }
- //printf("length=%i\n", len);
- //Calc 2 digit prefix
- int prefix=0;
- //Keep chopping until there are only 2 digits left
- while (cc4>99)
- {
- cc4 = cc4 / 10; //chop off last digit
- }
- prefix = cc4;
- //Check all the stuff (length, prefix, valid)
- //AMEX 15 digits, prefix 34/37
- //MC 16 digits, prefix 51-55
- //Visa 13/16 digits, prefix 40-49
- if (len == 15 && (prefix == 34 || prefix == 37))
- {
- printf("AMEX\n");
- } else if (len == 16 && (prefix >= 51 && prefix <= 55))
- {
- printf("MASTERCARD\n");
- } else if ((len==13 ||len==16) && (prefix >= 40 && prefix <= 49))
- {
- printf("VISA\n");
- } else
- {
- printf("INVALID\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement