Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <stdio.h>
- #import <cs50.h>
- //Daniel Lawson 9/19/17
- int main(void){
- printf("O hai! ");
- double input = 0;
- do{
- printf("How much change is owed?\n");//prompt for change
- input = get_double();//store it
- }while(input<0);//repeat if invalid
- int money = (int)(input*100);//convert to pennies
- int counter = 0;//keep track of how many coins it has taken
- int coins[] = {25,10,5,1}; //array that stores the various types if coins
- int coin =0; //keep track of which coin you are on
- while(money > 0){//repeat until all money has been taken into account
- int currentCoin = coins[coin]; //get the value of the coin you are on (25, or 10,5,1)
- counter += money / currentCoin; //increase the counter by how many times the current coin goes into the remaining amount of money
- money %= currentCoin; //reduce money to the remainder of the previous division
- coin++; //move to the next coin
- }
- printf("%d/n",counter); //print the minimum change required
- }
- /*
- desired output example:
- O hai! How much change is owed?
- 0.41
- 4
- */
Advertisement
Add Comment
Please, Sign In to add comment