SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Console Input And Output
Exercise:
Programming Projects
Question:7 | ISBN:9780132830317 | Edition: 5

Question

Write a program 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, . . . 90, 95, or 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:

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

1 nickels.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

/* 1 Quaters= 25 cents
   1 Dimes=10 cents
   1 Nickel=5 cents
   1 Penny=1 cent
 */
/*Vending Machine accept only 1 dollar coins.so 1 DOLLAR=100 cents*/
import java.util.*;
public class VendingMachine {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Scanner input =new Scanner(System.in);
int amount, quarters, dimes, nickels, pennies,exchange;

System.out.println("Enter the price of the(from 25 cents to a dollar, in 5-cent increments): ");
 amount=input.nextInt();
 
 exchange= 100-amount;
 
 System.out.println("Amout in Return:"+exchange+"cents");
 
 quarters=exchange/25;
 exchange=exchange%25;
 
 dimes=exchange/10;
 exchange=exchange%10;
 
 nickels=exchange/5;
 exchange=exchange%5;
 
 pennies=exchange;
 
 System.out.println("Quarters:"+quarters+" Dimes:"+dimes+" Nickels:"+nickels+" Pennies:"+pennies);
    }

}
 

0 0

Discussions

Post the discussion to improve the above solution.