/**** * * This program makes change, given an amount of purchase and an amount * tendered. The output is a total amount of change, followed by change in five * denominations of money: dollars, quarters, dimes, nickels, and pennies. All * inputs and outputs are in integer cents. * * The program processes multiple transactions, until the user inputs a * negative value for the amount of purchase. * * Author: Gene Fisher (gfisher@calpoly.edu) * Created: 15apr11 * Modified: 19apr11 * */ #include /** * Return the correct number of dollars in change for the given amount of * cents. */ int get_dollars(int cents) { return cents / 100; } /** * Return the correct number of quarters in change for the given amount of * cents. */ int get_quarters(int cents) { return cents % 100 / 25; } /** * Return the correct number of dimes in change for the given amount of * cents. */ int get_dimes(int cents) { return cents % 100 % 25 / 10; } /** * Return the correct number of nickels in change for the given amount of * cents. */ int get_nickels(int cents) { return cents % 100 % 25 % 10 / 5; } /** * Return the correct number of pennies in change for the given amount of * cents. */ int get_pennies(int cents) { return cents % 100 % 25 % 10 % 5; } int main() { /* * Declare program variables to hold the purchase amount, amount tendered, * and total amount of change. */ int purchase; /* Amount of purchase */ int tendered; /* Amount tendered */ int change; /* Total change due */ /* * Prompt the user for the amount of purchase, for the first transaction. */ printf("Input the amount of the purchase, in cents (negative value quits): "); /* * Input the amount of the first purchase. */ scanf("%d", &purchase); /* * Loop through transactions, while the user enters non-negative values for * the amount purchased. Note that the first time through the loop, the * purchase amount is entered before the loop starts. At the end of the * loop, the purchase amount is input again, and the loop continues. */ while (purchase >= 0) { /* * Prompt for the amount tendered. */ printf("Input the amount tendered, in cents: "); /* * Input the amount tendered. */ scanf("%d", &tendered); /* * Output a blank line, for nice formatting. */ printf("\n"); /* * Compute the total amount of change due, in cents. Assume that this * value is non-negative. */ change = tendered - purchase; /* * Output the total change amount, followed by a blank line. */ printf("Total change due = %d\n\n", change); /* * Compute and print the correct change for each denomination. */ printf("Change in dollars through pennies is:\n"); printf(" %d dollars\n", get_dollars(change)); printf(" %d quarters\n", get_quarters(change)); printf(" %d dimes\n", get_dimes(change)); printf(" %d nickels\n", get_nickels(change)); printf(" %d pennies\n", get_pennies(change)); /* * Input the amount of purchase again. */ printf("\nInput the amount of the purchase, in cents (negative value quits): "); scanf("%d", &purchase); } return 0; }