In: Computer Science
In Java:
Implement a program that directs a cashier how to give change. The program has two inputs: the amount due and the amount received from the customer.
Display the dollars, quarters, dimes, nickels, and pennies that the customer should receive in return. In order to avoid roundoff errors, the program user should supply both amounts in pennies (for example 274 instead of 2.74).
Solution:-
import java.util.Scanner;
public class makingChange
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
double amt;
int ten,five,one,quarter,dime,nickel,totalCents;
double amtdue,amtpaid;
System.out.println("Please enter the amount due :");
amtdue=scan.nextDouble();
System.out.println("Please enter the amount paid :");
amtpaid=scan.nextDouble();
amt=amtpaid-amtdue;
totalCents=(int)amt;
totalCents= (int) (100*amt);
ten=(totalCents/1000);
five=(int) ((amt-(ten*10))/5); //(totalCents/500);
one=(int) ((amt-(ten*10)-(five*5))/1);
quarter=(int) ((amt-(ten*10)-(five*5)-(one*1))/.25);
dime=(int) ((amt-(ten*10)-(five*5)-(one*1)-(quarter*.25))/.10);
nickel=(int) ((amt-(ten*10)-(five*5)-(one*1)-(quarter*.25)-(dime*.10))/.05);
System.out.println("Ten dollar bills: " + ten);
System.out.println("Five dollar bills: " + five);
System.out.println("One dollar bills: " + one);
System.out.println("Quarters: " + quarter);
System.out.println("Dimes: " + dime);
System.out.println("Nickels: " + nickel);
}
}

Thanking you.