So I am trying to find a code to let you enter an amount and it will output a coin amount with the least amount of numbers (ex. 220 coins =/= 220 pennies, instead 220 coins = 2 Dollars 2 Dimes) This is the code i have so far but it seems to not like my use of modula: import math change = int(input()) dollars = change % 100 quarters = math.remainder(dollars) % 25 dimes = math.remainder(quarters) % 10 nickels = math.remainder(dimes) % 5 pennies = math.remainder(dimes) % 1 dollars_modula = dollars // 1000 if dollars == 100: print('Dollar') elif dollars == 200 or dollars == 300 or dollars == 400 or dollars == 500 or dollars == 600 or dollars == 700 or dollars == 800 or dollars == 900 or dollars == 1000: print('Dollar') elif dollars <= dollars_modula: print('Dollars') elif quarters == 25: print('Quarter') elif quarters == 25 or quarters == 50 or quarters == 75: print('Quarters') elif dimes == 10: print('Dime') elif dimes == 20 or dimes == 30 or dimes == 40 or dimes == 60 or dimes == 70 or dimes == 80 or dimes == 90: print('Dimes') elif nickels == 10: print('Nickel') elif nickels == 15 or nickels == 35 or nickels == 45 or nickels == 65 or nickels == 85 or nickels == 95: print('Nickels') elif change == 1: print('Penny') elif change == 2 or change == 3 or change == 4: print('Pennies') else: print('No change')
So I am trying to find a code to let you enter an amount and it will output a coin amount with the least amount of numbers (ex. 220 coins =/= 220 pennies, instead 220 coins = 2 Dollars 2 Dimes) This is the code i have so far but it seems to not like my use of modula:
import math
change = int(input())
dollars = change % 100
quarters = math.remainder(dollars) % 25
dimes = math.remainder(quarters) % 10
nickels = math.remainder(dimes) % 5
pennies = math.remainder(dimes) % 1
dollars_modula = dollars // 1000
if dollars == 100:
print('Dollar')
elif dollars == 200 or dollars == 300 or dollars == 400 or dollars == 500 or dollars == 600 or dollars == 700 or dollars == 800 or dollars == 900 or dollars == 1000:
print('Dollar')
elif dollars <= dollars_modula:
print('Dollars')
elif quarters == 25:
print('Quarter')
elif quarters == 25 or quarters == 50 or quarters == 75:
print('Quarters')
elif dimes == 10:
print('Dime')
elif dimes == 20 or dimes == 30 or dimes == 40 or dimes == 60 or dimes == 70 or dimes == 80 or dimes == 90:
print('Dimes')
elif nickels == 10:
print('Nickel')
elif nickels == 15 or nickels == 35 or nickels == 45 or nickels == 65 or nickels == 85 or nickels == 95:
print('Nickels')
elif change == 1:
print('Penny')
elif change == 2 or change == 3 or change == 4:
print('Pennies')
else:
print('No change')
Algorithm to calculate the amount in dollars:
- It takes the input amount in cents.
- It calculates the number of dollars by using integer division (//) and updates the change amount.
- It calculates the number of quarters, dimes, nickels, and pennies in a similar way.
- It then prints out the denominations and their respective quantities, pluralizing the name if there's more than one of a particular coin.
This code will give you the least number of coins needed to represent the given amount of change.
Step by step
Solved in 4 steps with 2 images