Problem Set 1 (mario-less, mario-more, cash, credit)

Mario less

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

/*
 * File Name: mario.c
 * Programer: Julio C Ornelas
 * Program that creates a pyramid in C using # symbols
 * pyramid has to be between 1 and 8 in size
 * if user enters a number less than 1 or greater than 8 the
 * program will keep asking the user for a valid numer.
 */

int main(void)
{
    //declarations
    string hash = "#";
    string space = " ";
    int counter = 0;
    int userNum = 0;
    int numberOfSpaces = 0;
    int numberOfHash = 0;

    //get user input
    do
    {
        userNum = get_int("Height: ");
    }
    while (userNum < 1 || userNum > 8);

    numberOfSpaces = userNum;
    numberOfHash = 1;
    //calculation
    while (counter < userNum)
    {
        numberOfSpaces--;
        for (int i = 0; i < numberOfSpaces; i++)
        {
            printf("%s", space);
        }

        for (int i = 0; i < numberOfHash; i++)
        {
            printf("%s", hash);
        }
        numberOfHash++;
        printf("\n");
        counter++;

    }//while (counter < userNum)
}

Mario more

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

/*
 * File Name: mario.c
 * Programer: Julio C Ornelas
 * Program that creates a pyramid in C using # symbols
 * pyramid has to be between 1 and 8 in size
 * if user enters a number less than 1 or greater than 8 the
 * program will keep asking the user for a valid number
 * then the program skips 2 spaces and starts building a new
 * pyramid in the oposite direction
 */

int main(void)
{
    //declarations
    string hash = "#";
    string space = " ";
    int counter = 0;
    int userNum = 0;
    int numberOfSpaces = 0;
    int numberOfHash = 0;
    const int numberOfSpacesInBetween = 2;

    //get user input
    do
    {
        userNum = get_int("Height: ");
    }
    while (userNum < 1 || userNum > 8);

    numberOfSpaces = userNum;
    numberOfHash = 1;
    //calculation
    while (counter < userNum)
    {
        numberOfSpaces--;
        for (int i = 0; i < numberOfSpaces; i++) //this loop prints the spaces before the pyramid
        {
            printf("%s", space);
        }

        for (int i = 0; i < numberOfHash; i++) //this loop prints the # symbol **Left part or steps**
        {
            printf("%s", hash);
        }

        for (int i = 0; i < numberOfSpacesInBetween; i++) //this loop prints the spaces in between the two pyramids
        {
            printf("%s", space);
        }

        for (int i = 0; i < numberOfHash; i++) //this loop prints the # symbols from the oposite side **right part or steps**
        {
            printf("%s", hash);
        }
        numberOfHash++; //increase the number of steps for the nest level from the top down
        counter++;


        printf("\n");

    }//while (counter < userNum)
}

Cash

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

/*
 * File Name: cash.c
 * Programer: Julio C Ornelas
 */

int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);

int main(void)
{
    // Ask how many cents the customer is owed
    int cents = get_cents();

    // Calculate the number of quarters to give the customer
    int quarters = calculate_quarters(cents);
    cents = cents - quarters * 25;

    // Calculate the number of dimes to give the customer
    int dimes = calculate_dimes(cents);
    cents = cents - dimes * 10;

    // Calculate the number of nickels to give the customer
    int nickels = calculate_nickels(cents);
    cents = cents - nickels * 5;

    // Calculate the number of pennies to give the customer
    int pennies = calculate_pennies(cents);
    cents = cents - pennies * 1;

    // Sum coins
    int coins = quarters + dimes + nickels + pennies;

    // Print total number of coins to give the customer
    printf("%i\n", coins);
    //printf("%i\n", cents);
}

int get_cents(void)
{
    int userInput = 0;
    do
    {
        userInput = get_int("Change owed: ");
    }
    while (userInput < 0);
    return userInput;
}

int calculate_quarters(int cents)
{
    int totalQuarters = cents / 25;
    return totalQuarters;
}

int calculate_dimes(int cents)
{
    int totalDimes = cents / 10;
    return totalDimes;
}

int calculate_nickels(int cents)
{
    int totalNickels = cents / 5;
    return totalNickels;
}

int calculate_pennies(int cents)
{
    int totalPennies = cents / 1;
    return totalPennies;
}

Credit

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

/*
 * File Name: credit.c
 * Programer: Julio C Ornelas
 */

int main(void)
{
    //declarations
    long userCard = 0;
    string output = "";
    int counter = 0;
    int firstDigit = 0;
    int secondDigit = 0;
    int firstNumberSum = 0;
    int secondNumberSum = 0;
    int secondDigitTimesTwo = 0;
    int validationSum = 0;
    long cardNumber = 0;



    //get user input
    do
    {
        userCard = get_long("Number: ");
    }
    while (userCard <= 0);
    cardNumber = userCard;

    //calculation
    while (userCard > 1)
    {
        firstDigit = userCard % 10;
        //printf("__%i", firstDigit); // Validation this line is use to check that the correct information is calculated *Can delete*
        userCard = userCard / 10;
        firstNumberSum = firstNumberSum + firstDigit;
        counter++;


        secondDigit = userCard % 10;
        //printf("_%i", secondDigit); //Validation this line is use to check that the correct information is calculated *Can delete*
        userCard = userCard / 10;
        secondDigitTimesTwo = secondDigit * 2;

        if (secondDigitTimesTwo >= 10)
        {
            secondDigitTimesTwo = (secondDigitTimesTwo % 10) + 1;
        }
        //printf("---%i\n", secondDigitTimesTwo); //Validation this line is use to check that the correct information is calculated *Can delete*
        secondNumberSum = secondNumberSum + secondDigitTimesTwo;

        counter++;
    }

    validationSum = firstNumberSum + secondNumberSum;

    if (validationSum % 10 == 0)
    {
        if (cardNumber >= 300000000000000 && cardNumber < 390000000000000)
        {
            output = "AMEX";
        }
        else if (cardNumber >= 5100000000000000 && cardNumber <= 5900000000000000)
        {
            output = "MASTERCARD";
        }
        else
        {
            output = "VISA";
        }
    }
    else
    {
        output = "INVALID";
    }


    //output
    if (counter == 13 || counter == 15 || counter == 16 || counter == 14)
    {
        printf("%s\n", output);
        //printf("second digit sum: %i\n", secondNumberSum); //used to check the result
        //printf("first digit sum: %i\n", firstNumberSum); //used to check the result
        //printf("%i",validationSum); //used to check the result
        //printf("%i", counter);
    }
    else
    {
        output = "INVALID";
        printf("%s\n", output);
        //printf("%i",validationSum);//used to check the result
        //printf("%i", counter);
    }

}
//369421438430814
//5673598276138003

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *