Guest User

Untitled

a guest
Jul 11th, 2018
211
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cs50.h>
  2. #include <stdio.h>
  3.  
  4. int main(void)
  5. {
  6.     // Get the number
  7.     long long int number = get_long_long("Number:");
  8.     // Determine it's length
  9.     long long int a = number;
  10.     int counter = 0;
  11.     while (a > 0)
  12.     {
  13.         a /= 10;
  14.         counter++;
  15.     }
  16.     // Turn it into an array
  17.     int arr[counter], c = counter - 1;
  18.     while (c >= 0)
  19.     {
  20.         arr[c] = number % 10;
  21.         number /= 10;
  22.         c--;
  23.     }
  24.     // Check if it's a valid credit card number
  25.     int sum = 0;
  26.     for (int i = counter - 2; i >= 0; i -= 2)
  27.     {
  28.         if (arr[i] * 2 / 10.0 > 1)
  29.         {
  30.             printf("%i\n", arr[i] * 2 / 10);
  31.             printf("%i\n", arr[i] * 2 % 10);
  32.             sum += arr[i] * 2 / 10;
  33.             sum += arr[i] * 2 % 10;
  34.         }
  35.         else
  36.         {
  37.             sum += arr[i] * 2;
  38.         }
  39.     }
  40.     for (int j = counter - 1; j >= 0; j -= 2)
  41.     {
  42.         sum += arr[j];
  43.     }
  44.     printf("%i\n", sum);
  45.     if (sum % 10 != 0)
  46.     {
  47.         printf("INVALID\n");
  48.     }
  49.     // Determine it's type
  50.     else
  51.     {
  52.        if (arr[0] == 3 && (arr[1] == 4 || arr[1] == 7))
  53.         {
  54.             printf("AMEX\n");
  55.         }
  56.         else if (arr[0] == 5 && (arr[1] == 1 || arr[1] == 2 || arr[1] == 3 || arr[1] == 4 || arr[1] == 5))
  57.         {
  58.             printf("MASTERCARD\n");
  59.         }
  60.         else
  61.         {
  62.         printf("VISA\n");
  63.         }
  64.     }
  65. }
RAW Paste Data