Advertisement
jamestha3d

cs50 credit

Sep 24th, 2022 (edited)
1,541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3.  
  4. int main(void)
  5. {
  6.     long number = get_long("Number: ");
  7.  
  8.     //if(number is less than 15 or greater than 16)
  9.  
  10.     //find how many digits number is
  11.     int digits = 0;
  12.     long copy = number;
  13.     int first_digit = 0;
  14.     int first_2_digits = 0;
  15.     while (copy > 0)
  16.     {
  17.         copy = copy / 10;
  18.         digits += 1;
  19.         if (copy < 100 && copy > 10)
  20.         {
  21.             first_2_digits = copy;
  22.         }
  23.         if (copy < 10 && copy > 0)
  24.         {
  25.             first_digit = copy;
  26.         }
  27.     }
  28.  
  29.     if (digits == 13 || digits == 15 || digits == 16)
  30.     {
  31.         copy = number * 10;
  32.         int checksum = 0;
  33.         //get the sum of every other number * 2
  34.         while(copy > 0)
  35.         {
  36.             copy = copy / 100;
  37.             int answer = (copy % 10) * 2;
  38.             if (answer > 9)
  39.             {
  40.                 answer = answer % 10 + answer / 10;
  41.             }
  42.             checksum += answer;
  43.         }
  44.  
  45.         copy = number;
  46.         while (copy > 0)
  47.         {
  48.             checksum += copy % 10;
  49.             copy = copy / 100;
  50.  
  51.         }
  52.  
  53.         if (checksum % 10 == 0)
  54.         {
  55.             if (digits == 15 && (first_2_digits == 34 || first_2_digits == 37))
  56.             {
  57.                 printf("AMEX\n");
  58.             }
  59.             else if (digits == 13 && first_digit == 4)
  60.             {
  61.                 printf("VISA\n");
  62.             }
  63.             else
  64.             {
  65.                 if (first_digit == 4)
  66.                 {
  67.                     printf("VISA\n");
  68.                 }
  69.                 else if (first_2_digits == 51 || first_2_digits == 52 || first_2_digits == 53 || first_2_digits == 54 || first_2_digits == 55)
  70.                 {
  71.                     printf("MASTERCARD\n");
  72.                 }
  73.                 else
  74.                 {
  75.                     printf("INVALID\n");
  76.                 }
  77.             }
  78.         }
  79.         else
  80.         {
  81.             printf("INVALID\n");
  82.         }
  83.     }
  84.     else
  85.     {
  86.         printf("INVALID\n");
  87.     }
  88.  
  89.     //calculate checksum
  90.     //check card length and starting digits
  91.     //print AMEX, MASTERCARD, VISA or INVALID
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement