25.2k views
0 votes
Create a script that will determine how many of each currency type are needed to make change for a given amount of dollar and cents. Input Asks the user for a dollar and cents amount as a single decimal number. Output The program should indicate how many of each of these are needed for the given amount: $20 bills $10 bills $5 bills $1 bills Quarters ($0.25 coin) Dimes ($0.10 coin) Nickels ($0.05 coin) Pennies ($0.01 coin) If a dollar or coin is not needed (its quantity required is 0), do not print it.

User Nishani
by
5.2k points

1 Answer

5 votes

Answer:

The program in Python is as follows:

dollar = float(input("Dollars: "))

t20bill = int(dollar//20)

dollar -= t20bill * 20

t10bill = int(dollar//10)

dollar -= t10bill * 10

t5bill = int(dollar//5)

dollar -= t5bill * 5

t1bill = int(dollar//1)

dollar-= t1bill * 1

qtr = int(dollar//0.25)

dollar -= qtr * 0.25

dime = int(dollar//0.10)

dollar -= dime * 0.10

nkl = int(dollar//0.05)

dollar -= nkl * 0.05

pny = round(dollar/0.01)

if t20bill != 0: print(t20bill,"$20 bills")

if t10bill != 0: print(t10bill,"$10 bills")

if t5bill != 0: print(t5bill,"$5 bills")

if t1bill != 0: print(t1bill,"$1 bills")

if qtr != 0: print(qtr,"quarters")

if dime != 0: print(dime,"dimes")

if nkl != 0: print(nkl,"nickels")

if pny != 0: print(pny,"pennies")

Step-by-step explanation:

This gets input for dollars

dollar = float(input("Dollars: "))

Calculate the number of $20 bills

t20bill = int(dollar//20)

Calculate the remaining dollars

dollar -= t20bill * 20

Calculate the number of $10 bills

t10bill = int(dollar//10)

Calculate the remaining dollars

dollar -= t10bill * 10

Calculate the number of $5 bills

t5bill = int(dollar//5)

Calculate the remaining dollars

dollar -= t5bill * 5

Calculate the number of $1 bills

t1bill = int(dollar//1)

Calculate the remaining dollars

dollar-= t1bill * 1

Calculate the number of quarter coins

qtr = int(dollar//0.25)

Calculate the remaining dollars

dollar -= qtr * 0.25

Calculate the number of dime coins

dime = int(dollar//0.10)

Calculate the remaining dollars

dollar -= dime * 0.10

Calculate the number of nickel coins

nkl = int(dollar//0.05)

Calculate the remaining dollars

dollar -= nkl * 0.05

Calculate the number of penny coins

pny = round(dollar/0.01)

The following print the number of bills or coins. The if statement is used to prevent printing of 0

if t20bill != 0: print(t20bill,"$20 bills")

if t10bill != 0: print(t10bill,"$10 bills")

if t5bill != 0: print(t5bill,"$5 bills")

if t1bill != 0: print(t1bill,"$1 bills")

if qtr != 0: print(qtr,"quarters")

if dime != 0: print(dime,"dimes")

if nkl != 0: print(nkl,"nickels")

if pny != 0: print(pny,"pennies")

User Scrollex
by
5.5k points