119k views
4 votes
Write a program (Console or GUI) that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and 1 dollar, in 5-cent increments (25, 30, 35,…,95, 100), and the machine accepts only a single dollar bill to pay for the item. For example, a possible sample dialog might be the following:

Output: Enter price of item (from 25 cents to a dollar, in 5-cent increments):

45 You bought an item for 45 cents and gave me a dollar, so your change is 2 quarters, 0 dimes, and

User Feisky
by
5.6k points

1 Answer

7 votes

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

Step-by-step 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

Write a program (Console or GUI) that determines the change to be dispensed from a-example-1
User Brian Kelley
by
6.2k points