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...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT