In: Computer Science
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-cents increments (25, 30, 35, . . . 90, 95, or 100), and the machine accepts only a single dollar bill to pay for the item. (Explain with algorithms if you can please!)
Executable code:
import java.util.Scanner;
public class VendingMachine{
public static void main(String args[])
{
Scanner keyboard=new Scanner(System.in);
int n1,amount,n2,n3,n4,n5;
System.out.println("Enter price of item (from 20 cents to a dollar,
in 5-cents):");
n1=keyboard.nextInt();
System.out.println("You bought an item for "+n1+" cents and gave me
adollar");
System.out.println("So your change id");
amount=100-n1;
n2=amount/25;
amount=amount%25;
n3=amount/10;
amount=amount%10;
n4=amount/5;
amount=amount%5;
n5=amount;
System.out.println(n2+" quarters");
System.out.println(n3+" domes");
System.out.println(n4+" nickels");
System.out.println(n5+" pennies");
}
}
Output: