Answer:
Here is the JAVA program:
import java.util.Scanner; //to accept input from user
public class Main {
// class name
public static void main(String [] args) { // start of main method
Scanner input = new Scanner(System.in); // creates Scanner class object to take input from user
System.out.println("Please Enter the Cost of the Item: "); // prompts user to enter the cost of item
double itemCost = input.nextDouble(); //scans and reads the input value of item cost
System.out.println("Please Enter the Amount Paid: "); // prompts user to enter the amount paid
double amount = input.nextDouble(); //scans and reads the value of amount from user
int change = (int)(amount * 100 - itemCost * 100); //compute the remaining amount i.e. change
System.out.println("Change Owed: " + change / 100.0); // displays the owed change
int quarters = change / 25; // computes the value for quarters
change = change % 25; // compute the quarters remaining
int dimes = change / 10; // computes dimes
change = change % 10; // computes dimes remaining
int nickels = change / 5; // computes nickels
change = change % 5; // compute nickels remaining
int pennies = change; // computes pennies
System.out.println("Quarters: " + quarters); // displays computed value of quarters
System.out.println("Dimes: " + dimes); // displays value of dimes
System.out.println("Nickels: " + nickels); // displays value of nickels
System.out.println("Pennies: " + pennies); }} //displays value of pennies
Explanation:
I will explain the program with an examples.
Suppose the user enters 4.57 as cost of the item and 5.00 as amount paid. Then the program works as follows:
Change is computed as
change = (int)(amount * 100 - itemCost * 100);
This becomes;
change = (int)(5.00 * 100 - 4.57 * 100)
= 500 - 457
change = 43
Now the change owed is computed as:
change / 100.0 = 43/100.0 = 0.43
Hence change owed = 0.43
Next quarters are computed as:
quarters = change / 25;
This becomes:
quarters = 43/25
quarters = 1
Now the remaining is computed as:
change = change % 25;
This becomes:
change = 43 % 25;
change = 18
Next the dimes are computed from remaining value of change as:
dimes = change / 10;
dimes = 18 / 10
dimes = 1
Now the remaining is computed as:
change = change % 10;
This becomes:
change = 18 % 10
change = 8
Next the nickels are computed from remaining value of change as:
nickels = change / 5;
nickels = 8 / 5
nickels = 1
Now the remaining is computed as:
change = change % 5;
This becomes:
change = 8 % 5
change = 3
At last the pennies are computed as:
pennies = change;
pennies = 3
So the output of the entire program is:
Change Owed: 0.43 Quarters: 1 Dimes: 1
Nickels: 1
Pennies: 3
The screenshot of the output is attached.