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...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT