68.8k views
2 votes
Write a program that asks the user for an integer between 0 and 100 that represents a number of cents. Convert that number of cents to the equivalent number of quarters, dimes, nickels, and pennies. Then output the maximum number of quarters that will fit the amount, then the maximum number of dimes that will fit into what then remains, and so on. If the amount entered is negative, write an error message and quit. Amounts greater than 100 are OK (the user will get many quarters.) Use extended assembly instructions and exception handler services. Enter number of cents: 67 Number of quarters:2 Number of dimes :1 Number of nickels :1 Number of pennies :2

1 Answer

1 vote

Solution :

import
$\text{math}$

#First we need to define the value of each coin


$\text{penny=1}$


$\text{nickel}$ = 5


$\text{dime}$ = 10


$\text{quarter}$ = 25

#Here we define
$\text{the variables}$ to hold the
$\text{max}$ number of each coin


$\text{quarter}$s = 0


$\text{dime}$s = 0


$\text{nickel}$s = 0


$\text{pennys}$ = 0


$\text{cents}$ = int(
$\text{inpu}t $("Please enter an
$\text{amount}$ of money you have in
$\text{cents}$: "))

if
$\text{cents}$
$>0$ and
$\text{cents}$
$<=$ 100:

if
$\text{cents}$ >= 25:

quarters =
$\text{cents}$ / quarter


$\text{cents}$ =
$\text{cents}$ % quarter

if
$\text{cents}$ >= 10:

dimes = cents/dime


$\text{cents}$ =
$\text{cents}$ % dime

if
$\text{cents}$ >= 5:

nickels =
$\text{cents}$ /nickel


$\text{cents}$ =
$\text{cents}$ % nickel

if
$\text{cents}$ > 0:

pennys =
$\text{cents}$ / penny


$\text{cents}$ = 0

print("The coins are: "

"\
$\text{nQuarters}$", math.floor(quarters),

"\
$\text{nDimes}$", math
$\text{.floor}$(dimes), "\
$\text{nNickels}$", math
$\text{.floor}$(nickels), "\\Pennies", math
$\text{.floor}$(pennys))

else:

print("wrong value")

User Tim Fletcher
by
4.3k points