Skip to content

Instantly share code, notes, and snippets.

@dasunsucharith
Created April 15, 2022 07:25
Show Gist options
  • Save dasunsucharith/c53cbe70e61fc023a7b8aa0df514ad23 to your computer and use it in GitHub Desktop.
Save dasunsucharith/c53cbe70e61fc023a7b8aa0df514ad23 to your computer and use it in GitHub Desktop.
My solution to CS50 2022 psets-1 cash
#include <cs50.h>
#include <stdio.h>
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);
}
int get_cents(void)
{
int cents;
do {
cents = get_int("Change Owed: ");
} while (cents < 0);
return cents;
}
int calculate_quarters(int cents)
{
int quarters = cents / 25;
return quarters;
}
int calculate_dimes(int cents)
{
int dimes = cents / 10;
return dimes;
}
int calculate_nickels(int cents)
{
int nickels = cents / 5;
return nickels;
}
int calculate_pennies(int cents)
{
int pennies = cents / 1;
return pennies;
}
@dasunsucharith
Copy link
Author

are u sure

Yes sure

@Mrfire10
Copy link

it dinDNT SHOW SMILES LIAR

@umm641
Copy link

umm641 commented Sep 22, 2023

IT'S DON'T WORK.IT'S CAN'T COMPILE

@aya285
Copy link

aya285 commented Nov 5, 2023

Thanks you i was struggling with the input when it is 73 or 28 and it worked !

@MotiveCK
Copy link

MotiveCK commented Mar 8, 2024

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

int get_cents(void);
int calculate_coins(int cents, int denomination);

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

// Define denominations
int denominations[] = {25, 10, 5, 1};

// Initialize total coins count
int coins = 0;

// Calculate the number of each denomination to give the customer
for (int i = 0; i < 4; i++) {
    int count = calculate_coins(cents, denominations[i]);
    cents -= count * denominations[i];
    coins += count;
}

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

}

int get_cents(void)
{
int cents;

do {
    cents = get_int("Change Owed: ");
} while (cents < 0);

return cents;

}

int calculate_coins(int cents, int denomination)
{
int count = cents / denomination;
return count;
}
`

@rachitowski
Copy link

rachitowski commented Apr 16, 2024

#include <cs50.h>
#include <stdio.h>
#define quarter 25
#define dime 10
#define nickel 5
#define penny 1
int main(void)
{
int change;
do
{
change = get_int("Change: ");
}
while (change < 1);
int quarter_coins = change / quarter;
change = change - (quarter_coins * quarter);
int dime_coins = change / dime;
change = change - (dime * dime_coins);
int nickel_coins = change / nickel;
change = change - (nickel * nickel_coins);
int penny_coins = change;
printf("%i\n", quarter_coins + dime_coins + nickel_coins + penny_coins);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment