/* Read an integer value between 0 and 100 (inclusive) representing a purchase amount in cents. Produce an error message if the input value is out of range. If the input is valid, determine the amount of change that would be received from one dollar, and print the number of quarters, dimes, nickels and pennies that should be returned. Maximize the coins with the highest value. Author: Alan Renaud, CS100 TA Date: 5 July 1999 */ import java.io.*; public class P1_4B { public static void main (String args[]) { int price; // purchase amount int change; // change received int q; // number of quarters int d; // number of dimes int n; // number of nickels int p; // number of pennies // initialize Text object in to read from standard input. TokenReader in = new TokenReader(System.in); // prompt user to enter in the purchase amount of the item. System.out.print("Enter the purchase amount [0-100]: "); System.out.flush(); price = in.readInt(); // print error message if price is out of range. if ((price<0)||(price>100)) System.out.println("That purchase amount is invalid."); else { change = 100 - price; // determine number of quarters, dimes, nickels, pennies. q = change/25; d = (change%25)/10; n = ((change%25)%10)/5; p = (((change%25)%10)%5); // print change System.out.println("\nYour change of " + change + " cents is given as: "); System.out.print( q + " Quarter" + (q != 1 ? "s" : "")); System.out.print("\n" + d + " Dime" + (d != 1 ? "s" : "")); System.out.print("\n" + n + " Nickel" + (n != 1 ? "s" : "")); System.out.print("\n" + p + " Penn" + (p != 1 ? "ies\n" : "y\n")); System.out.flush(); } // Wait for user to enter input to ensure console window remains visible in.waitUntilEnter(); }// End main } // End P1_4B /* Sample Outputs: ------------------------------------------------ Enter the purchase amount [0-100]: 275 That purchase amount is invalid. ------------------------------------------------ Enter the purchase amount [0-100]: 36 Your change of 64 cents is given as: 2 Quarters 1 Dime 0 Nickels 4 Pennies ------------------------------------------------ Enter the purchase amount [0-100]: 59 Your change of 41 cents is given as: 1 Quarter 1 Dime 1 Nickel 1 Penny */