DityaSen

Credit CS50 PSET1

Feb 9th, 2023
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | Source Code | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3.  
  4. int main(void)
  5. {
  6.     //Getting Card Number
  7.     long num = get_long("Number: ");
  8.  
  9.     //Length Count
  10.  
  11.     long dum = num;
  12.     int digit = 0;
  13.     while (dum>0)
  14.     {
  15.         dum = dum/10;
  16.         digit++;
  17.     }
  18.  
  19.     //Check Length
  20.  
  21.     if(digit != 13 && digit != 15 && digit != 16)
  22.     {
  23.         printf("INVALID\n");
  24.         return 0;
  25.     }
  26.  
  27.  
  28.  
  29.     long dum2 = num;
  30.     int sumodd = 0;
  31.     int sumeven = 0;
  32.     int rem1 = 0;
  33.     int rem2 = 0;
  34.     int digit1 = 0;
  35.     int digit2 = 0;
  36.  
  37.     do
  38.     {   //Remove the Last Digit
  39.         rem1 = dum2%10;
  40.         sumodd += rem1;
  41.         dum2 = dum2/10;
  42.  
  43.         //Remove 2nd Last Digit
  44.         rem2 = dum2%10;
  45.         dum2 = dum2/10;
  46.  
  47.         //Double the Digit and Add Sum of Digits
  48.         rem2 = rem2*2;
  49.         digit1 = rem2%10;
  50.         rem2 = rem2/10;
  51.         sumeven = sumeven + digit1 + digit2;
  52.     }
  53.     while (dum2>0);
  54.  
  55.     int final = sumeven + sumodd;
  56.  
  57.     //Checking if final is a multiple of 10
  58.  
  59.     long dum3 = num;
  60.  
  61.     if (final%10 == 0)
  62.     {
  63.         do
  64.         {
  65.             dum3 = dum3/10;
  66.         }
  67.         while(dum>0);
  68.  
  69.         if((dum3 / 10 == 5) && (0 < dum3 % 10 && dum3 % 10 < 6))
  70.         {
  71.             printf("MASTERCARD\n");
  72.  
  73.         }
  74.         else if (dum3 == 34 || dum3 == 37)
  75.         {
  76.             printf("AMEX\n");
  77.         }
  78.         else if ((dum3/10) == 4)
  79.         {
  80.             printf("VISA\n");
  81.         }
  82.         else
  83.         {
  84.             printf("INVALID\n");
  85.         }
  86.     }
  87.     else
  88.     {
  89.         printf("INVALID\n");
  90.         return 0;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment