143k views
5 votes
Write a program that prompts the user to input an integer that represents cents. The program will then calculate the smallest combination of coins that the user has. For example, 27 cents is 1 quarter, 0 nickel, and 2 pennies. That is 27=1*25+0*5+2*1.

Example program run 1:
Enter number of cents: 185
Pennies: 0
Nickels: 0
Dimes: 1
Quarters: 7
Example program run 2:
Enter number of cents: 67
Pennies: 2
Nickels: 1
Dimes: 1

User Yaxlat
by
4.2k points

1 Answer

6 votes

Answer:

The program in Python is as follows:

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

qtr = int(cents/25)

cents = cents - qtr * 25

dms = int(cents/10)

cents = cents - dms * 10

nkl = int(cents/5)

pny = cents - nkl * 5

print("Pennies: ",pny)

print("Nickels: ",nkl)

print("Dimes: ",dms)

print("Quarters: ",qtr)

Step-by-step explanation:

The program in Python is as follows:

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

This calculate the number of quarters

qtr = int(cents/25)

This gets the remaining cents

cents = cents - qtr * 25

This calculates the number of dimes

dms = int(cents/10)

This gets the remaining cents

cents = cents - dms * 10

This calculates the number of nickels

nkl = int(cents/5)

This calculates the number of pennies

pny = cents - nkl * 5

The next 4 lines print the required output

print("Pennies: ",pny)

print("Nickels: ",nkl)

print("Dimes: ",dms)

print("Quarters: ",qtr)

User Shuki Avraham
by
4.3k points