Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a total change amount: ");
int totalAmount = in.nextInt();
int dollars = totalAmount / 100;
System.out.println(dollars + " Dollar" + (dollars == 1 ? "" : "s"));
totalAmount %= 100;
int quarters = totalAmount / 25;
System.out.println(quarters + " Quarter" + (quarters == 1 ? "" : "s"));
totalAmount %= 25;
int dimes = totalAmount / 10;
System.out.println(dimes + " Dime" + (dimes == 1 ? "" : "s"));
totalAmount %= 10;
int nickels = totalAmount / 5;
System.out.println(nickels + " Nickel" + (nickels == 1 ? "" : "s"));
int pennies = totalAmount % 5;
System.out.println(pennies + " Penn" + (pennies == 1 ? "y" : "ies"));
}
}
Comments
Leave a comment