192k views
4 votes
Write a program that prompts the user to input an integer that represents the money in cents. The program will the calculate the smallest combination of coins that the user has. For example, 42 cents is 1 quarter, 1 dime, 1 nickel, and 2 pennies. Similarly, 49 cents is 1 quarter, 2 dimes, and 4 pennies

1 Answer

3 votes

Answer:

The program in Python is as follows:

cents = int(input("Cent: "))

quarter = int(cents/25)

if quarter > 1:

print(str(quarter)+" quarters")

elif quarter == 1:

print(str(quarter)+" quarter")

cents = cents - quarter * 25

dime = int(cents/10)

if dime>1:

print(str(dime)+" dimes")

elif dime == 1:

print(str(dime)+" dime")

cents = cents - dime * 10

nickel = int(cents/5)

if nickel > 1:

print(str(nickel)+" nickels")

elif nickel == 1:

print(str(nickel)+" nickel")

penny = cents - nickel * 5

if penny > 1:

print(str(penny)+" pennies")

elif penny == 1:

print(str(penny)+" penny")

Step-by-step explanation:

This line prompts user for input in cents

cents = int(input("Cent: "))

This line calculates the number of quarters in the input amount

quarter = int(cents/25)

This following if statement prints the calculated quarters

if quarter > 1:

print(str(quarter)+" quarters")

elif quarter == 1:

print(str(quarter)+" quarter")

cents = cents - quarter * 25

This line calculates the number of dime in the input amount

dime = int(cents/10)

This following if statement prints the calculated dime

if dime>1:

print(str(dime)+" dimes")

elif dime == 1:

print(str(dime)+" dime")

cents = cents - dime * 10

This line calculates the number of nickels in the input amount

nickel = int(cents/5)

This following if statement prints the calculated nickels

if nickel > 1:

print(str(nickel)+" nickels")

elif nickel == 1:

print(str(nickel)+" nickel")

This line calculates the number of pennies in the input amount

penny = cents - nickel * 5

This following if statement prints the calculated pennies

if penny > 1:

print(str(penny)+" pennies")

elif penny == 1:

print(str(penny)+" penny")

User Eric Renouf
by
5.2k points