Answer to Question #275008 in Java | JSP | JSF for java

Question #275008

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.


1
Expert's answer
2021-12-03T12:34:50-0500
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"));
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS