37.3k views
2 votes
Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies.

1 Answer

0 votes

Answer:

Here is the Python program:

def FewestCoinVals(): # function to output change using fewest coins

total_change = int(input('Enter change amount in pennies: '))

#prompts user to enter total change amount in pennies

if total_change <= 0: # if value of total_change is less or equal to 0

print('no change') # prints no change

else: # if value of total_change is greater than 0

dollars = total_change // 100

#calculate value of dollars by dividing total_change by 100

total_change %= 100 #take mod of value of total_change with 100

#calculate value of quarters by dividing total_change by 100

quarters = total_change // 25

total_change %= 25 #take mod of value of total_change with 25

#calculate value of dimes by dividing total_change by 100

dimes = total_change // 10

total_change %= 10 #take mod of value of total_change with 10

#calculate value of nickels by dividing total_change by 100

nickels = total_change // 5

total_change %= 5 #take mod of value of total_change with 5

pennies = total_change

#value of pennies is equal to value of total_change

#the if elif statement checks for the singular and plural coin names

if dollars >1:

print('dollars: ',dollars)

elif dollars ==1:

print('dollar: ',dollars)

if quarters > 1:

print('quarters: ',quarters)

elif quarters ==1:

print('quarter: ',quarters)

if dimes >1:

print('dimes: ',dimes)

elif dimes ==1:

print('dime: ',dimes)

if nickels >1:

print('nickels: ',nickels)

elif nickels ==1:

print('nickel: ',nickels)

if pennies >1:

print('pennies: ',pennies)

elif pennies ==1:

print('penny: ',pennies)

#calls the FewestCoinVals() function

FewestCoinVals()

Step-by-step explanation:

The program prompts the user to enter change amount in pennies and has int() function to convert input value to integer. If the value entered is less than or equal to 0 it displays message "no change", otherwise it converts the value using fewest coins. The formulas for converting to each type is given in the code. It uses if and elif statements to check if the final value of total_change has singular or plural coin names such as penny or pennies. The program along with output is attached as the screen shot.

Write a program with total change amount in pennies as an integer input, and output-example-1
Write a program with total change amount in pennies as an integer input, and output-example-2
User Nuhman
by
3.5k points