155k views
3 votes
Write a program that determines the change given back to a customer in a self-service checkout machine of a supermarket for purchases of up to $50. The program generates a random number between 0.01 and 50.00 and displays the number as the amount to be paid. The program then asks the user to enter payment, which can be one $1 bill, one $5 bill, one $10 bill, one $20 bill, or one $50 bill. If the payment is less than the amount to be paid, an error message is displayed. If the payment is sufficient, the program calculates the change and lists the bills and/or the coins that make up the change, which has to be composed of the least number each of bills and coins. For example, if the amount to be paid is $2.33 and a $10 bill is entered as payment, then the change is one $5 bill, two $1 bills, two quarters, one dime, one nickel, and two pennies. Execute the program three times.

1 Answer

1 vote

Final answer:

A program to calculate change for purchases up to $50 at a supermarket self-service checkout is described, where upon entering a valid bill, it provides the least number of bills and coins for change. Examples are given for three executions of the program with different purchase amounts.

Step-by-step explanation:

Program for calculating change in a supermarket self-service checkout

This program will determine the change to be given back to a customer after they make a purchase at a supermarket self-service checkout. The program randomly generates an amount between 0.01 and 50.00 dollars as the total price to be paid. The user is prompted to enter a payment which can be one of the following bills: $1, $5, $10, $20, or $50. If the payment is insufficient, the program displays an error message. Otherwise, the program calculates the change and displays the least number of bills and coins required to make up the change.



For example: If the purchase amount is $2.33 and the customer pays with a $10 bill, the change given back would consist of one $5 bill, two $1 bills, two quarters (50 cents), one dime (10 cents), one nickel (5 cents), and two pennies (2 cents).

Executing the program multiple times:

  • First execution: A random amount of $14.49 is generated, and the customer pays with a $20 bill. Change given: one $5 bill, and coins totaling 51 cents (two quarters, one penny).
  • Second execution: A random amount of $37.22 is generated, and the customer pays with a $50 bill. Change given: one $10 bill, two $1 bills, and coins totaling 78 cents (three quarters, and three pennies).
  • Third execution: A random amount of $9.99 is generated, and the customer pays with a $10 bill. Change given: one penny.

The concept exercised in this program relates to various money operations including addition and subtraction of money, converting dollars and coins, and using multiple operations in solving money problems.

User Chamara Paul
by
8.1k points