Respuesta :
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 function
Scanner input =new Scanner(System.in); Â // creates Scanner class object
int amount, quarters, dimes, nickels, pennies,change; Â //declare variables
System.out.println("Enter the price of the(from 25 cents to a dollar, in 5-cent increments): "); Â //prompts user to enter the price
amount=input.nextInt(); Â //reads amount from user
change= 100-amount; Â //computes change
System.out.println("You bought an item for " + amount+" cents and gave me a dollar, so your change is :"); Â
quarters=change/25; Â //computes quarters
change=change%25; Â //computes quarter remaining
if(quarters == 1) Â // if quarter is equal to 1
System.out.println(quarters+ " quarter"); Â //prints quarter value in singular
else if (quarters>1) Â // if value of quarters is greater than 1
System.out.println(quarters+" quarters"); Â Â //prints plural quarters value
dimes=change/10; Â //computes dimes
if(dimes == 1) Â // if value of dime is equal to 1
System.out.println(dimes+ " dime"); Â //prints single value of dimes
else if (dimes>1) Â //if value of dimes is greater than 1
System.out.println(dimes+" dimes"); Â //prints plural value of dimes
change=change%10; Â //computes dimes remaining
nickels=change/5; Â //computes nickels
if(nickels == 1) Â //if value of nickels is equal to 1
System.out.println(nickels+ " nickel"); Â //prints single value of nickels
else if (nickels>1) Â //if value of nickels is greater than 1
System.out.println(nickels+" nickels"); Â //prints plural value of nickels
change=change%5; Â Â //computes nickels remaining
pennies=change; Â //computes pennies
if(pennies == 1) Â //if value of pennies is equal to 1
System.out.println(pennies+ " penny"); Â //prints single value of pennies
else if (pennies>1) Â Â //if value of pennies is greater than 1
System.out.println(pennies+" pennies"); Â Â } } Â //prints plural value of pennies
Explanation:
I will explain the program with an example: Â
Suppose amount= 75 Â
Then change = 100 - amount= 100 - 75 = 25 Â
change = 25 Â
quarters = change/25 = 25/25 = 1 Â
quarters = 1 Â
change = change % 25 = 25%25 = 0 Â
dimes = 0/10 = 0 Â
Now all other values are 0. Â
Now the following line is printed on screen: Â
You bought an item for 75 cents and gave me a dollar. Your change is: Â
Now program moves to if part Â
if quarters == 1 Â
This is true because value of quarter is 1 Â
sot "quarter" Â is displayed with the value of quarters
Now the following line is printed on screen: Â
1 quarter Â
So the complete output of the program is: Â
You bought an item for 75 cents and gave me a dollar. Your change is: Â
1 quarter
