Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int main(void)
- {
- // Acquire credit card number as string for invalid character search iteration
- string credit_card_1 = get_string("CC#: ");
- // Convert credit card string to long long integer.
- long long credit_card_2 = atoll(cc_1);
- // Tally for the sum of every second digit x 2
- int sum1 = 0;
- // Establish divisors increased my a factor of 100 every iteration to isolate the current number place affected
- long long upper_divisor = 100;
- long long lower_divisor = 10;
- //For the string length of the credit card number
- for (int i = 0; i < strlen(credit_card_1); i++)
- {
- // Islolate and remove numbers prior to the affected number place
- int upper_isolate = credit_card_2 % upper_divisor;
- // Isolate and remove the numbers after the affected number place
- int lower_isolate = upper_isolate % lower_divisor;
- int lower_removal = upper_isolate - lower_isolate;
- // Divide by number place to reduce number to a single digit
- int isolated_number_place_1 = lower_removal / lower_divisor;
- // Double isolated number
- int current_digit = isolated_number_place_1 * 2;
- // Add doubled isolated number to the sum1 tally
- sum1 = sum1 + current_digit;
- // Multiply divisors to isolated the next highest number place
- upper_divisor = upper_divisor * 100;
- lower_divisor = lower_divisor * 100;
- // Diagnostic: print each new sum tally to ensure the math is correct.
- printf("%i\n", sum1);
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement