Question

In: Computer Science

Program in Java code Write a program with total change amount in pennies as an integer...

Program in Java code

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.

.Ex1: If the input is: 0

the output is:    No change           

Ex2: If the input is:    45  

the output is:   1 Quarter

2 Dimes

Solutions

Expert Solution

import java.util.Scanner;

public class Convert {

        public static void main(String[] args) {
                
                Scanner sc = new Scanner(System.in);
                
                // reading number of pennies
                System.out.print("Enter change amount in pennies : ");
                int amount = sc.nextInt();
                
                int remaining = amount;
                
                // calculating number of dollars
                int dollars = remaining/100;
                remaining = remaining % 100;
                
                // calculating number of quarters
                int quarters = remaining / 25;
                remaining = remaining % 25;
                
                // calculating number of dimes
                int dimes = remaining / 10;
                remaining = remaining % 10;
                
                // calculating number of nickels
                int nickels = remaining / 5;
                remaining = remaining % 5;
                
                // calculating number of pennies
                int pennies = remaining ;
                
                // if currency is greater than zero then print that currency 
                // in singular and plural
                if(dollars > 0) {
                        System.out.println(dollars==1?"1 Dollar":dollars+" Dollars");
                }
                if(quarters > 0) {
                        System.out.println(quarters==1?"1 Quarter":quarters+" Quarters");
                }
                if(dimes > 0) {
                        System.out.println(dimes==1?"1 Dime":dimes+" Dimes");
                }
                if(nickels > 0) {
                        System.out.println(nickels==1?"1 Nickel":nickels+" Nickels");
                }
                if(pennies > 0) {
                        System.out.println(pennies==1?"1 Penny":pennies+" Pennies");
                }
                
                // checking if all are zeros and prints no change
                if(dollars ==0 && quarters ==0&& dimes==0&&nickels==0&&pennies==0) {
                        System.out.println("No change");
                }
                
                sc.close();

        }

}

Code

Output

Testcase #1

Testcase #2

Testcase #3


Related Solutions

For Java 3.25 LAB: Exact change Write a program with total change amount in pennies as...
For Java 3.25 LAB: Exact change 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. Ex: If the input is: 0 the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes...
Write a program with total change amount as an integer input, and output the change using...
Write a program with total change amount 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. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes c++ please
Write a program in pyton with total change amount as an integer input, and output the...
Write a program in pyton with total change amount 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. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes Can you...
6.32 LAB: Exact change - functions Write a program with total change amount as an integer...
6.32 LAB: Exact change - functions Write a program with total change amount as an integer input that outputs 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. Ex: If the input is: 0 or less, the output is: no change Ex: If the input is: 45 the output is: 1 quarter 2 dimes...
USE PYTHON 4.15 LAB: Exact change Write a program with total change amount as an integer...
USE PYTHON 4.15 LAB: Exact change Write a program with total change amount 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. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter...
Needs to be in JAVA. Write a Java program that accepts the total amount of cars...
Needs to be in JAVA. Write a Java program that accepts the total amount of cars sold and total sales amount of a car salesperson for a given month. The salesperson’s paycheck is computed as follows: a. Every sales person gets 10% (commission) of total sales b. Sales totals greater than $50,000 get 5% of total sales amount c. 8 or more cars sold earns the salesperson an extra 3% Please remove 30% (taxes) of the gross pay and list...
C++ 19.32 LAB: Exact change - functions Write a program with total change amount as an...
C++ 19.32 LAB: Exact change - functions Write a program with total change amount as an integer input that outputs 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. Ex: If the input is: 0 or less, the output is: no change Ex: If the input is: 45 the output is: 1 quarter 2...
Java This is background information First, code this project. Write a program that determines the change...
Java This is background information First, code this project. Write a program that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and a dollar, in 5-cent increments (25, 30, 35, ..., 90, 95, or 100) and the machine only accepts a single dollar bill to pay for the item. For example, a possible dialogue with the user might beEnter price of item(from 25 cents to a dollar, in...
C++ please 6.32 LAB: Exact change - functions Write a program with total change amount as...
C++ please 6.32 LAB: Exact change - functions Write a program with total change amount as an integer input that outputs 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. Ex: If the input is: 0 or less, the output is: no change Ex: If the input is: 45 the output is: 1 quarter...
Write a java program that calculates the total amount of money a person has made over...
Write a java program that calculates the total amount of money a person has made over the last year. The program will prompt the user for dollar amounts showing how much the user has made per job. Some users will have had more than one job, make sure your program allows for this. The program will print out the total made (all jobs) and will print out the federal and state taxes based on the total made. For this program,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT