-
-
Save sameekapdi/e0ee149465e54210ebb284afab2d0b75 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 <cs50.h> | |
#include <stdio.h> | |
#include <math.h> | |
int main(void) | |
{ | |
//declare variables | |
float change; | |
int coins, cents; | |
//Get user input, must be non-negative. | |
do | |
{ | |
printf("O hai! How much change is owed?\n"); | |
change = get_float(); | |
}while (change <0); | |
//convert into int and minimise floating point imprecision | |
cents = (int)round(change*100); | |
coins = 0; | |
//Get how many 25c coins are needed, get the remainder via modulo | |
coins += cents / 25; | |
cents %=25; | |
//Get how many 10c coins are needed, get the remainder via modulo | |
coins+= cents / 10; | |
cents %= 10; | |
//Get how many 5c coins are needed, get the remainder via modulo | |
coins += cents / 5; | |
cents %= 5; | |
//remainder coins are pennies | |
coins += cents; | |
//Print result | |
printf("%i\n", coins); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment