126k views
4 votes
Create a program that calculates the coins needed to make change for the specified number of cents.

User Kfb
by
3.5k points

1 Answer

6 votes

Answer:

cents = int(input("Enter the number of cents: "))

quarters = cents // 25

cents -= quarters * 25

dimes = cents // 10

cents -= dimes * 10

nickels = cents // 5

cents -= nickels * 5

pennies = cents % 5

print("quarters: " + str(quarters) + ", dimes: " + str(dimes) + ", nickels: " + str(nickels) + ", pennies: " + str(pennies))

print("In total " + str(quarters + dimes + nickels + pennies) + " coins needed.")

Step-by-step explanation:

*The code is in Python.

Ask the user to enter the number of cents

Calculate the number of quarters, dimes, nickels, and pennies in the given amount. Print each of them and the total amount of coins

Note that floor division, //, is used to get the corresponding coin values. For example, if the entered value is 51,

quarters = 51 // 25 = 2 (returns the lower integer, if the result is not an integer)

User Toris
by
4.2k points