Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- int main(void)
- {
- long number = get_long("Number: ");
- //if(number is less than 15 or greater than 16)
- //find how many digits number is
- int digits = 0;
- long copy = number;
- int first_digit = 0;
- int first_2_digits = 0;
- while (copy > 0)
- {
- copy = copy / 10;
- digits += 1;
- if (copy < 100 && copy > 10)
- {
- first_2_digits = copy;
- }
- if (copy < 10 && copy > 0)
- {
- first_digit = copy;
- }
- }
- if (digits == 13 || digits == 15 || digits == 16)
- {
- copy = number * 10;
- int checksum = 0;
- //get the sum of every other number * 2
- while(copy > 0)
- {
- copy = copy / 100;
- int answer = (copy % 10) * 2;
- if (answer > 9)
- {
- answer = answer % 10 + answer / 10;
- }
- checksum += answer;
- }
- copy = number;
- while (copy > 0)
- {
- checksum += copy % 10;
- copy = copy / 100;
- }
- if (checksum % 10 == 0)
- {
- if (digits == 15 && (first_2_digits == 34 || first_2_digits == 37))
- {
- printf("AMEX\n");
- }
- else if (digits == 13 && first_digit == 4)
- {
- printf("VISA\n");
- }
- else
- {
- if (first_digit == 4)
- {
- printf("VISA\n");
- }
- else if (first_2_digits == 51 || first_2_digits == 52 || first_2_digits == 53 || first_2_digits == 54 || first_2_digits == 55)
- {
- printf("MASTERCARD\n");
- }
- else
- {
- printf("INVALID\n");
- }
- }
- }
- else
- {
- printf("INVALID\n");
- }
- }
- else
- {
- printf("INVALID\n");
- }
- //calculate checksum
- //check card length and starting digits
- //print AMEX, MASTERCARD, VISA or INVALID
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement