7 minute read

Chapter 1 C PSet

Exercise 3.2: Credit - Program to ask users for a credit card number and check whether it is VISA, MC, or AMEX implementing Luhn’s algorithm

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

int main(void)
{
    //prompt user for credt card number using do while and do not accept numbers below the min 13 digits for it to be a valid credit card
    long n;
    do
    {
        n = get_long("CC number:");
    }
    while(n < 1000000000000);

    //Luhn's algorithm first multiply every other digit by two starting on the second digit from last
    // maybe I can start by taking the whole number, dividing by 10 iteratively
    // assuming the number was 4230 then maybe i could keep dividing and ask for integer result so that the rest of the digits disappear
    //first implementation might be kind of dumb and ape but will work and I can make if else statements for the different card lengths similar to the do while loop

    // 13-digit VISA
    if (n >= 1000000000000LL && n <= 9999999999999LL)
    {
        // extract d1…d13
        long d1  = (n / 1000000000000LL) % 10;  // 10^12
        long d2  = (n /  100000000000LL) % 10;  // 10^11
        long d3  = (n /   10000000000LL) % 10;  // 10^10
        long d4  = (n /    1000000000LL) % 10;  // 10^9
        long d5  = (n /     100000000LL) % 10;  // 10^8
        long d6  = (n /      10000000LL) % 10;  // 10^7
        long d7  = (n /       1000000LL) % 10;  // 10^6
        long d8  = (n /        100000LL) % 10;  // 10^5
        long d9  = (n /         10000LL) % 10;  // 10^4
        long d10 = (n /          1000LL) % 10;  // 10^3
        long d11 = (n /           100LL) % 10;  // 10^2
        long d12 = (n /            10LL) % 10;  // 10^1
        long d13 = (n /             1LL) % 10;  // 10^0

        // Luhn’s algorithm: double every 2nd digit from right, sum digits
        int sum = 0;
        // non-doubled digits
        sum += d1 + d3 + d5 + d7 + d9 + d11 + d13;
        // doubled digits: d2,d4,d6,d8,d10,d12
        long d[6] = { d2, d4, d6, d8, d10, d12 };
        for (int i = 0; i < 6; i++)
        {
            int x = d[i] * 2;
            sum += (x / 10) + (x % 10);
        }

        if (sum % 10 == 0)
            printf("VISA\n");
        else
            printf("NOT VALID\n");
    }
    // 15-digit AMEX
    else if (n >= 100000000000000LL && n <= 999999999999999LL)
    {
        // extract d1…d15
        long d1  = (n / 100000000000000LL) % 10;  // 10^14
        long d2  = (n /  10000000000000LL) % 10;  // 10^13
        long d3  = (n /   1000000000000LL) % 10;  // 10^12
        long d4  = (n /    100000000000LL) % 10;  // 10^11
        long d5  = (n /     10000000000LL) % 10;  // 10^10
        long d6  = (n /      1000000000LL) % 10;  // 10^9
        long d7  = (n /       100000000LL) % 10;  // 10^8
        long d8  = (n /        10000000LL) % 10;  // 10^7
        long d9  = (n /         1000000LL) % 10;  // 10^6
        long d10 = (n /          100000LL) % 10;  // 10^5
        long d11 = (n /           10000LL) % 10;  // 10^4
        long d12 = (n /            1000LL) % 10;  // 10^3
        long d13 = (n /             100LL) % 10;  // 10^2
        long d14 = (n /              10LL) % 10;  // 10^1
        long d15 = (n /               1LL) % 10;  // 10^0

        // Luhn sum
        int sum = 0;
        // non-doubled: d1,d3,d5,d7,d9,d11,d13,d15
        sum += d1 + d3 + d5 + d7 + d9 + d11 + d13 + d15;
        // doubled: d2,d4,d6,d8,d10,d12,d14
        long d_amex[7] = { d2, d4, d6, d8, d10, d12, d14 };
        for (int i = 0; i < 7; i++)
        {
            int x = d_amex[i] * 2;
            sum += (x / 10) + (x % 10);
        }

        if (sum % 10 == 0)
            printf("AMEX\n");
        else
            printf("NOT VALID\n");
    }
    // 16-digit VISA/MASTERCARD
    else if (n >= 1000000000000000LL && n <= 9999999999999999LL)
    {
        // extract d1…d16
        long d1  = (n / 1000000000000000LL) % 10;  // 10^15
        long d2  = (n /  100000000000000LL) % 10;  // 10^14
        long d3  = (n /   10000000000000LL) % 10;  // 10^13
        long d4  = (n /    1000000000000LL) % 10;  // 10^12
        long d5  = (n /     100000000000LL) % 10;  // 10^11
        long d6  = (n /      10000000000LL) % 10;  // 10^10
        long d7  = (n /       1000000000LL) % 10;  // 10^9
        long d8  = (n /        100000000LL) % 10;  // 10^8
        long d9  = (n /         10000000LL) % 10;  // 10^7
        long d10 = (n /          1000000LL) % 10;  // 10^6
        long d11 = (n /           100000LL) % 10;  // 10^5
        long d12 = (n /            10000LL) % 10;  // 10^4
        long d13 = (n /             1000LL) % 10;  // 10^3
        long d14 = (n /              100LL) % 10;  // 10^2
        long d15 = (n /               10LL) % 10;  // 10^1
        long d16 = (n /                1LL) % 10;  // 10^0

        // Luhn sum
        int sum = 0;
        // non-doubled: d2,d4,...,d16? No—count from right: positions 1,3,5... so non-doubled = d2? Actually for 16 digits, rightmost d16 pos1(non-doubled), d15 pos2(doubled), etc.
        // So non-doubled: d16, d14, d12, d10, d8, d6, d4, d2
        sum += d16 + d14 + d12 + d10 + d8 + d6 + d4 + d2;
        // doubled: d15,d13,d11,d9,d7,d5,d3,d1
        long d_mc[8] = { d15, d13, d11, d9, d7, d5, d3, d1 };
        for (int i = 0; i < 8; i++)
        {
            int x = d_mc[i] * 2;
            sum += (x / 10) + (x % 10);
        }

        if (sum % 10 == 0)
        {
            // check prefix for card type
            if      (d1 == 4)               printf("VISA\n");
            else if ((d1 == 5) && (d2 >= 1 && d2 <= 5)) printf("MASTERCARD\n");
            else                               printf("UNKNOWN\n");
        }
        else
            printf("NOT VALID\n");
    }
    else
    {
        printf("INVALID LENGTH\n");
    }




}

Exercise 3.1: Cash (Program to ask users for change due in cents and display minimum number of coins (quarters, nickels, and pennies))

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

int main(void)
{
    //ask user how much change is due
    int n;
    do
    {
        n = get_int("change due? in cents:");
    }
    while(n < 1);

    // compute number of quarters
    int q = n / 25;

    //computer number of dimes based on remainder from q
    int r0 = n - q * 25;
    int d = r0 / 10;

    //computer number of nickles based on remainder from d
    int r1 = n - q * 25 - d * 10;
    int ni = r1 / 5;

    //computer number of pennies based on remainder from ni
    int r2 = n - q * 25 - d * 10 - ni * 5;
    int p = r2 / 1;

    // print number of coins
    printf("%i\n", q + d + ni + p);
}

Exercise 2.2: Mario More w Comments

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

//2 adding three more inputs to make double pyramids

void print_row(int spaces, int bricks, int yes, int brick2, int spaces2);

int main(void)
{
    //prompt user for height at least 1 using do while loop

    int n;
    do
    {
        n = get_int("Height?");

    }
    while(n < 1 || n > 8);
    //print row function using for loop
    for(int i = 0; i < n; i++)
    {
        print_row(n - i - 1, i + 1, 1, i + 1, n - i - 1);
    }
}

//2 adding three more inputs to make double pyramids
void print_row(int spaces, int bricks, int yes, int brick2, int spaces2)
{
    //print spaces
    for(int i = 0; i < spaces; i++)
    {
        printf(" ");
    }
    // print bricks
    for (int i = 0; i < bricks; i++)
    {
        printf("#");
    }
    //2print constant space
    for(int i = 0; i < yes; i++)
    {
        printf("  ");
    }
    //2print bricks
    for(int i = 0; i < brick2; i++)
    {
        printf("#");
    }
    //2print spaces
    for(int i = 0; i < spaces2; i++)
    {
        printf(" ");
    }
    printf("\n");
}

Exercise 2.1: Mario Less w Comments

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

//declaring the function print row
void print_row(int spaces, int bricks);

//typical starting function. outputs 0 for success and all others for errors. not sure why void is the placeholder
int main(void)

{
    //promt user for height need do while loop to avoid numbers under 1 and you define n first because you will need it again
    int n;
    do
    {
    n = get_int("Height?\n");

    }
    while(n < 1);

    //now the print_row function to print the pyramid

    for(int i = 0; i < n; i++)
    {
        print_row(n - i - 1, i + 1);
    }
}
//now the code to print bricks and spaces
void print_row(int spaces, int bricks)
{
    //print spaces
    for(int i = 0; i < spaces ; i++)
    {
        printf(" ");

    }
    //print bricks
    for(int i = 0; i < bricks ; i++)
    {
        printf("#");

    }
    printf("\n");

}

Exercise 2: Mario Less

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

void print_row(int spaces, int bricks);

int main(void)
{
    // Prompt the user for the pyramid's height
    int n;
    do
    {
        n = get_int("Height: ");
    }
    while (n < 1);

    // Print a pyramid of that height
    for (int i = 0; i < n; i++)
    {
        // Print row of bricks
        print_row(n - i - 1, i + 1);

    }
}

void print_row(int spaces, int bricks)
{
    // Print spaces
    for (int i = 0; i < spaces; i++)
    {
        printf(" ");
    }

    // Print bricks
        for (int i = 0; i < bricks; i++)
    {
        printf("#");
    }
    printf("\n");
}

Exercise 1: Hello World

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

int main(void)

{
    string name = get_string("what is your name?\n");
    printf("Hello %s\n", name);

}

Updated: