Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- #include <math.h>
- int main(void){
- float input_amount;
- int pennies;
- int coins = 0;
- // Get the user input
- do {
- printf("Enter amount of change due: ");
- input_amount = get_float();
- if (input_amount < 0) {
- printf("You must enter a non-negative number. Try again.\n");
- }
- }
- while (input_amount < 0);
- // Get total amount of pennies input is equal to (multiply float by 100. Round as floats are imprecise (420 = 419.9999...)
- pennies = round(input_amount * 100);
- // Amount of 50p's that go into amount of pennies
- coins += pennies / 50;
- pennies %= 50;
- // Amount of 20p's that go into amount of pennies
- coins += pennies / 20;
- pennies %= 20;
- // Amount of 10p's that go into amount of pennies
- coins += pennies / 10;
- pennies %= 10;
- // Amount of 5p's that go into amount of pennies
- coins += pennies / 5;
- pennies %= 5;
- // Amount of 2's that go into amount of pennies
- coins += pennies / 2;
- pennies %= 2;
- // Amount of 1p's that go into amount of pennies
- coins += pennies / 1;
- pennies %= 1;
- printf("Minimum amount of coins required: %i\n", coins);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement