Created
January 12, 2018 21:44
-
-
Save sojohnnysaid/8c5affacc7b001e79408625f07dcd2df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#include <cs50.h> | |
int main(void) | |
{ | |
// prompt user for an amount of change | |
float user_input; | |
int change, num_of_coins; | |
do | |
{ | |
printf("Enter the amount of change you want back: "); | |
user_input = get_float(); | |
} | |
while(user_input < 0); | |
change = round(user_input*100); | |
// keep track of coin used | |
num_of_coins = 0; | |
// always use the largest coin possible | |
// modulo math version is tricky | |
num_of_coins += change / 25; | |
change %= 25; | |
num_of_coins += change / 10; | |
change %= 10; | |
num_of_coins += change / 5; | |
change %= 5; | |
num_of_coins += change / 1; | |
change %= 1; | |
// print the final number of coins | |
printf("You entered:\t%.2f\n", user_input); | |
printf("least coins possible:\t%d\n", num_of_coins); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment