Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. int main(void)
  8. {
  9.     // Acquire credit card number as string for invalid character search iteration
  10.     string credit_card_1 = get_string("CC#: ");
  11.    
  12.     // Convert credit card string to long long integer.
  13.     long long credit_card_2 = atoll(cc_1);
  14.    
  15.     // Tally for the sum of every second digit x 2
  16.     int sum1 = 0;
  17.    
  18.     // Establish divisors increased my a factor of 100 every iteration to isolate the current number place affected
  19.     long long upper_divisor = 100;
  20.     long long lower_divisor = 10;
  21.    
  22.     //For the string length of the credit card number
  23.     for (int i = 0; i < strlen(credit_card_1); i++)
  24.     {
  25.         // Islolate and remove numbers prior to the affected number place
  26.         int upper_isolate = credit_card_2 % upper_divisor;
  27.        
  28.         // Isolate and remove the numbers after the affected number place
  29.         int lower_isolate = upper_isolate % lower_divisor;             
  30.         int lower_removal = upper_isolate - lower_isolate;
  31.        
  32.         // Divide by number place to reduce number to a single digit
  33.         int isolated_number_place_1 = lower_removal / lower_divisor;
  34.        
  35.         // Double isolated number
  36.         int current_digit = isolated_number_place_1 * 2;
  37.        
  38.         // Add doubled isolated number to the sum1 tally
  39.         sum1 = sum1 + current_digit;
  40.        
  41.         // Multiply divisors to isolated the next highest number place
  42.         upper_divisor = upper_divisor * 100;
  43.         lower_divisor = lower_divisor * 100;
  44.        
  45.         // Diagnostic: print each new sum tally to ensure the math is correct.
  46.         printf("%i\n", sum1);
  47.     }
  48.    
  49.     printf("\n");
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement