Advertisement
Guest User

creditJF

a guest
Apr 7th, 2020
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3.  
  4. int isvalid(long n);
  5.  
  6. int main(void)
  7. {
  8.     long ccnum = get_long("Enter Credit card number: \n");
  9.     int x = isvalid(ccnum);
  10.     if(x != 0)
  11.     {
  12.         printf("INVALID\n");
  13.     }
  14.     else if (x == 0 && (ccnum > 5099999999999999 && ccnum < 5600000000000000))
  15.     {
  16.     printf("MASTERCARD\n");
  17.     }
  18.     else if ( x == 0 && (ccnum > 339999999999999 && ccnum < 350000000000000))
  19.     {
  20.         printf("AMEX\n");
  21.     }
  22.     else if (x == 0 && (ccnum > 369999999999999 && ccnum < 380000000000000))
  23.     {
  24.         printf("AMEX\n");
  25.     }
  26.     else if ( x == 0 && (ccnum >3999999999999999 && ccnum < 4999999999999999))
  27.     {
  28.         printf("VISA\n");
  29.     }
  30. }
  31.  
  32. int isvalid(long n)
  33. {
  34.     if (n > 999999999999999 && n < 10000000000000000)
  35.     {
  36.         int x = (2 * (n % 10) + 2 * ((n % 1000 - n % 100) / 100)) + (2 * ((n % 100000 - n % 10000) / 10000)) + (2 * ((n % 10000000 - n % 1000000) / 1000000)) + (2 * ((n % 1000000000 - n % 100000000) / 100000000)) + (2 * ((n % 100000000000 - n % 10000000000)/ 10000000000)) + (2 * ((n % 10000000000000 - n % 1000000000000) / 1000000000000)) + (2 * ((n % 1000000000000000 - n % 100000000000000) / 100000000000000));
  37.         int y = (2 * ((n % 100 - n %10) / 10)) + (2 * ((n % 10000 - n % 1000) / 1000)) + (2 * ((n % 1000000 - n % 100000) / 100000)) + (2 * ((n % 100000000 - n % 10000000) / 10000000)) + (2 * ((n % 10000000000 - n % 1000000000) / 1000000000)) + (2 * ((n % 1000000000000 - n % 100000000000) / 100000000000)) + (2 * ((n % 100000000000000 - n % 10000000000000) / 10000000000000)) + (2 * ((n % 10000000000000000 - n % 1000000000000000) / 1000000000000000));
  38.         return (x + y) % 10;
  39.     }
  40.     else if (n > 99999999999999 && n < 1000000000000000)
  41.     {
  42.         int x = (2 * (n % 10)) + (2 * ((n % 1000 - n % 100) / 100)) + (2 * ((n % 100000 - n % 10000) / 10000)) + (2 * ((n % 10000000 - n % 1000000) / 1000000)) + (2 * ((n % 1000000000 - n % 100000000) / 100000000)) + (2 * ((n % 100000000000 - n % 10000000000)/ 10000000000)) + (2 * ((n % 10000000000000 - n % 1000000000000) / 1000000000000)) + (2 * ((n % 1000000000000000 - n % 100000000000000) / 100000000000000));
  43.         int y = (2 * ((n % 100 - n %10) / 10)) + (2 * ((n % 10000 - n % 1000) / 1000)) + (2 * ((n % 1000000 - n % 100000) / 100000)) + (2 * ((n % 100000000 - n % 10000000) / 10000000)) + (2 * ((n % 10000000000 - n % 1000000000) / 1000000000)) + (2 * ((n % 1000000000000 - n % 100000000000) / 100000000000)) + (2 * ((n % 100000000000000 - n % 10000000000000) / 10000000000000));
  44.         return ((x + y) % 10);
  45.     }  
  46.     else if (n > 999999999999 && n < 10000000000000)
  47.     {
  48.         int x = (2 * (n % 10) + 2 * ((n % 1000 - n % 100) / 100)) + (2 * ((n % 100000 - n % 10000) / 10000)) + (2 * ((n % 10000000 - n % 1000000) / 1000000)) + (2 * ((n % 1000000000 - n % 100000000) / 100000000)) + (2 * ((n % 100000000000 - n % 10000000000)/ 10000000000)) + (2 * ((n % 10000000000000 - n % 1000000000000) / 1000000000000));
  49.         int y = (2 * ((n % 100 - n %10) / 10)) + (2 * ((n % 10000 - n % 1000) / 1000)) + (2 * ((n % 1000000 - n % 100000) / 100000)) + (2 * ((n % 100000000 - n % 10000000) / 10000000)) + (2 * ((n % 10000000000 - n % 1000000000) / 1000000000)) + (2 * ((n % 1000000000000 - n % 100000000000) / 100000000000));
  50.         return (x + y) % 10;
  51.     }
  52.     else
  53.     {
  54.         return 1;
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement