Advertisement
noob339

Untitled

Oct 1st, 2021
1,356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <math.h>
  4.  
  5.  
  6. int main(void)
  7. {
  8.     //declare my variables, could do it all on one line but would exceed page width
  9.     long int ccNum, ccNum2, ccNum3, ccNum4, firstSetOfDigit, secondSetOfDigits, checkSum, passedCheckSum;
  10.     long int timesTwo, splitTimes, totalSum = 0, totalSum2 = 0, ccLength = 0;
  11.  
  12.     //This prompts user to enter their cc and copy's that original value into several similar variables
  13.     do
  14.     {
  15.         ccNum = get_long("What's your card number: ");
  16.         ccNum2 = ccNum; //do I need more variables? Idk but for now, it seems to help
  17.         ccNum3 = ccNum;
  18.         ccNum4 = ccNum;
  19.         ccNum /= 10;
  20.     }
  21.     while (ccNum < 0);
  22.  
  23.     //This is for the sum of every other number starting with the SECOND to last number of the cc
  24.     while (ccNum > 0)
  25.     {
  26.         firstSetOfDigit = ccNum % 10;
  27.         timesTwo = firstSetOfDigit * 2;
  28.         splitTimes = (timesTwo / 10) + (timesTwo % 10);
  29.         totalSum += splitTimes;
  30.         ccNum /= 100;
  31.  
  32.     }
  33.  
  34.     //This is for the sum of every other number starting with the last number of the cc
  35.     while (ccNum2 > 0)
  36.     {
  37.         secondSetOfDigits = ccNum2 % 10;
  38.         totalSum2 += secondSetOfDigits;
  39.         ccNum2 /= 100;
  40.     }
  41.  
  42.     //these calculations sum up the calculations from both while loops to get the sum
  43.     checkSum = totalSum + totalSum2;
  44.     passedCheckSum = checkSum % 10; //if check sum ends in 0 it should be divisible by 10 with no remainders
  45.  
  46.     //this gets the length of the card
  47.     while (ccNum3 > 0)
  48.     {
  49.         firstSetOfDigit = ccNum3 % 10;
  50.         ccNum3 /= 10;
  51.         ccLength++;
  52.     }
  53.  
  54.     //This loop gets the first two digits of the card number, simple as it is, it took some time to realize just how simple it was
  55.     for (int p = 0; p <= (ccLength - 3); p++)
  56.     {
  57.         ccNum4 /= 10;
  58.     }
  59.  
  60.     //This gives an prints an output based on the conditions below using the calculations from above
  61.     //VISA
  62.     if ((ccLength == 16 || ccLength == 13) && (passedCheckSum == 0) && (ccNum4 >= 40 && ccNum4 <= 49))
  63.     {
  64.         printf("VISA\n");
  65.     }
  66.     //AMEX
  67.     else if ((ccLength == 15) && (passedCheckSum == 0) && (ccNum4 == 34 || ccNum4 == 37))
  68.     {
  69.         printf("AMEX\n");
  70.     }
  71.     //MASTERCARD
  72.     else if ((ccLength == 16) && (passedCheckSum == 0) && (ccNum4 >= 51 && ccNum4 <= 55))
  73.     {
  74.         printf("MASTERCARD\n");
  75.     }
  76.     //INVALID
  77.     else
  78.     {
  79.         printf("INVALID\n");
  80.     }
  81. }
  82.  
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement