0

My code passed pset1/credit but I still think the there must be a way to make my code looks cleaner and more impact. If you have any idea please criticize the code. Thank you in advance.

#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
    //STEP 1
    //Get credit card number
    long c_number = get_long("Number: ");

    //change to int for showing
    long show_number = c_number;

    //digit numbers
    int digit = 0;

    //counter for checking digits
    long i = c_number;

    //Check digits
    while (i >= 1)
    {
        i = i / 10;
        digit++;
    }

    //print credit card number
    //printf("%li \n", show_number);

    //print digits
    //printf("digits: %i \n", digit);

    //STEP 2
    //Luhn’s algorithm

    //declare variables
    int n01 = (c_number / 1000000000000000) % 10;
    int n02 = (c_number / 100000000000000) % 10;
    int n03 = (c_number / 10000000000000) % 10;
    int n04 = (c_number / 1000000000000) % 10;
    int n05 = (c_number / 100000000000) % 10;
    int n06 = (c_number / 10000000000) % 10;
    int n07 = (c_number / 1000000000) % 10;
    int n08 = (c_number / 100000000) % 10;
    int n09 = (c_number / 10000000) % 10;
    int n10 = (c_number / 1000000) % 10;
    int n11 = (c_number / 100000) % 10;
    int n12 = (c_number / 10000) % 10;
    int n13 = (c_number / 1000) % 10;
    int n14 = (c_number / 100) % 10;
    int n15 = (c_number / 10) % 10;
    int n16 = (c_number / 1) % 10;


    // LOOOP
    long show_num = 0;
    long power = pow(10, digit);
    int temp_digit = 1;
    int sum_odd = 0;
    int sum_even = 0;

    for (long j = 1; j <= power; j *= 10)
    {
        if (temp_digit % 2 == 0)
        {
            show_num = ((show_number / j) % 10) * 2;
            if (show_num >= 10)
            {
                int show_num01 = show_num / 10;
                int show_num02 = show_num % 10;
                //printf("show_num01 = %i \n", show_num01);
                //printf("show_num02 = %i \n", show_num02);
                show_num = show_num01 + show_num02;
            }
            //printf("%li ", show_num);
            sum_odd = sum_odd + show_num;
        }
        else
        {
            show_num = (show_number / j) % 10;
            /*if (show_num > 10)
            {
                int show_num01 = show_num / 10;
                int show_num02 = show_num % 10;
                show_num = show_num01 + show_num02;
            }*/
            //printf("%li ", show_num);
            sum_even = sum_even + show_num;
        }

        temp_digit++;
    }

    int total_sum = sum_odd + sum_even;
    /*
        printf("\n");
        printf("%i\n", sum_odd);
        printf("%i\n", sum_even);

        printf("%i\n", total_sum);
        printf("\n");
    */

    //check card legit
    int last_digit = total_sum % 10;
    //printf("%i\n", last_digit);

    if (last_digit == 0 && n01 == 5)
    {
        if (n02 == 1 || n02 == 2 || n02 == 3 || n02 == 4 || n02 == 5)
        {
            if (digit == 16 || digit == 13)
            {
                printf("MASTERCARD\n");
            }
            else
            {
                printf("INVALID\n");
            }
        }
        else
        {
            printf("INVALID\n");
        }
    }
    else if (last_digit == 0 && n01 == 4 && digit == 16)
    {
        printf("VISA\n");
    }
    else if (last_digit == 0 && n02 == 3 && digit == 15)
    {
        if (n03 == 4 || n03 == 7)
        {
            printf("AMEX\n");
        }
        else
        {
            printf("INVALID\n");
        }
    }
    else
    {
        printf("INVALID\n");
    }

    /*
        printf("%i", n01);
        printf("%i", n02);
        printf("%i", n03);
        printf("%i", n04);
        printf("%i", n05);
        printf("%i", n06);
        printf("%i", n07);
        printf("%i", n08);
        printf("%i", n09);
        printf("%i", n10);
        printf("%i", n11);
        printf("%i", n12);
        printf("%i", n13);
        printf("%i", n14);
        printf("%i", n15);
        printf("%i\n", n16);
    */

}
0

A way to get the individual digits of a number is to use the % operator,

int number = 123456;

last_number = number % 10; // 123456 % 10 = 6 
number /= 10;  // 123456 / 10 = 12345
second_last_number = number % 10;  // 12345 % 10 = 5 

To understand the code remember the % operator returns the remainder of the division. So when you get the remainder of 123456 / 10 = 12345.6 so 6 is the last digit of your number. Then we divide it by 10 since 123456/10 will remove the last digit 6 since 123456 / 10 = 12345 with this I hope you can find a more well designed solution for this problem

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .