Question: I need to create a program that calculates change after a purchase. This will be a sequential algorithm, which is one of the simplest programming
I need to create a program that calculates change after a purchase. This will be a sequential algorithm, which is one of the simplest programming patterns. Your solution requires no branching or looping. Simply write a series of statements that will execute in order.
The program should ask for a purchase price and the amount of cash tendered. It should then determine how many of the following denomininations should be returned:
penny -$0.01
nickel - $0.05
dime - $0.10
quarter - $0.25
dollar $1.00
five -$5.00
ten - $10.00
twenty - $20.00
Here is some of the python I have written:
import math
price = float(input("Enter price of item: ")) #taking in price of item
cash = float(input("Enter cash tendered: ")) #taking in cash from user
change = cash - price # calculate change
change = int(change * 100) # multiply range by 100 to get an integer
print("{0:.2f}".format(price)) print("{0:.2f}".format(cash)) print("{0:.2f}".format(change))
quarters = 0 # defining variables for change dimes = 0 nickels = 0 pennies = 0 dollars = int(change/100) #calculate dollars fives = int(change/20) tens = int(change/10) twenties = int(change/5) cents = change - (dollars*100)
pennies = cents cents = cents - (nickels * 5) nickels = math.trunc(nickels * 5) dimes = math.trunc(cents/10) cents = cents - (dimes * 10) quarters = math.trunc(cents/25) cents = cents - (quarters * 25)
print("Dollars : " + str(dollars)) #output change print("Quarters : " + str(quarters)) print("Dimes : " + str(dimes)) print("Nickels : " + str(nickels)) print("Pennies : " + str(pennies))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
