import java.util.Scanner; /** * Reads a number of cents and prints out the proper change, in US coinage. * @author pollen */ public class HopeAndChange { public static void main(String[] args) { final int QUARTER_VALUE = 25; final int DIME_VALUE = 10; final int NICKEL_VALUE = 5; Scanner in = new Scanner(System.in); // Read in the number of cents. System.out.print("How many cents do you want? "); // We'll call our variable centsLeft, since we will decrease it as we go. int centsLeft = in.nextInt(); // Preface the output with an explanation. System.out.println("Here is your change:"); // Give quarters. int quarters = centsLeft / QUARTER_VALUE; // This division will round down. System.out.println(quarters + " quarter(s)"); centsLeft %= QUARTER_VALUE; // Take the remainder after dividing centsLeft by 25. // Give dimes. int dimes = centsLeft / DIME_VALUE; System.out.println(dimes + " dime(s)"); centsLeft %= DIME_VALUE; // Give nickels. int nickels = centsLeft / NICKEL_VALUE; System.out.println(nickels + " nickel(s)"); centsLeft %= NICKEL_VALUE; // Give pennies. System.out.println(centsLeft + " penny(s)"); } }