CSC111 Homework 2 Solutions 2014
--D. Thiebaut (talk) 11:20, 20 February 2014 (EST)
Problem #1
#hw2a.py
#Zoey Jingyi Sun (ch)
#Yipeng Lai (bq)
#Solution Program for Problem#1 in Homework#2
#
#This program is to breakdown the money entered by the user into bills and coins
#-------------------------------------------------------------------------------
#
#Get user input
amount=float(input( "Please enter the amount of money you want to withdraw:"))
#Print top bar
bar="+"+"-"*36+"+"
length=len(bar)
print(bar)
print("|","Total amount withdrawn: ","$%7.2f" % amount," |")
print("|"+" "*(length-2)+"|")
bill=int(amount)
coin=int((amount-bill)*100)
#Calculate bills numbers
BILL1=20
BILL2=10
BILL3=5
BILL4=1
#Calculate the numbers of $20-bill(s)
noBill= bill // BILL1
leftover= bill % BILL1
line1=" %2d" % noBill+" $20-bill(s)"
print ("|"+line1+" "*(length-len(line1)-2)+"|")
#Calculate the numbers of $10-bill(s)
noBill= leftover // BILL2
leftover= leftover % BILL2
line1=" %2d" % noBill+" $10-bill(s)"
print ("|"+line1+" "*(length-len(line1)-2)+"|")
#Calculate the numbers of $5-bill(s)
noBill= leftover // BILL3
leftover= leftover % BILL3
line1=" %2d" % noBill+" $5-bill(s)"
print ("|"+line1+" "*(length-len(line1)-2)+"|")
#Calculate the numbers of $1-bill(s)
noBill= leftover // BILL4
leftover= leftover % BILL4
line1=" %2d" % noBill+" $5-bill(s)"
print ("|"+line1+" "*(length-len(line1)-2)+"|")
#Print middle bar
print ("|"+"-"*19+" "*(length-21)+"|")
#Calculation of coins numbers
COIN1=25
COIN2=10
COIN3=5
COIN4=1
#Calculate the numbers of quarter(s)
noCoin= coin // COIN1
leftover= coin % COIN1
line2=" %2d" % noCoin+" quarter(s)"
print ("|"+line2+" "*(length-len(line2)-2)+"|")
#Calculate the numbers of dime(s)
noCoin= leftover // COIN2
leftover= leftover % COIN2
line2=" %2d" % noCoin+" dime(s)"
print ("|"+line2+" "*(length-len(line2)-2)+"|")
#Calculate the numbers of nickel(s)
noCoin= leftover // COIN3
leftover= leftover % COIN3
line2=" %2d" % noCoin+" nickel(s)"
print ("|"+line2+" "*(length-len(line2)-2)+"|")
#Calculate the numbers of cent(s)
noCoin= leftover // COIN4
leftover= leftover % COIN4
line2=" %2d" % noCoin+" cent(s)"
print ("|"+line2+" "*(length-len(line2)-2)+"|")
#Print bottom bar
print(bar)
Problem #2
#hw2b.py
#Zoey Jingyi Sun (ch)
#Yipeng Lai (bq)
#Solution Program for Problem#2 in Homework#2
#
#This program is to calculate the total balance of an investment growing at
#a fixed interest rate over ten years
#---------------------------------------------------------------------------
#Get user input
initial= float(input("Please enter the initial balance :"))
intRate= float(input("Please enter the interest rate (enter 0.03 for 3%):"))
print()
print("Table for an investment of $ %.2f"%initial,"at a rate of %.1f%%"%(intRate*100))
#Calculate the balance after 1 year
balance=initial*(1+intRate)
print("Balance after 1 year: $%.2f"%balance)
#Calculate the balance after 2 year
balance=balance*(1+intRate)
print("Balance after 2 year: $%.2f"%balance)
#Calculate the balance after 3 year
balance=balance*(1+intRate)
print("Balance after 3 year: $%.2f"%balance)
#Calculate the balance after 4 year
balance=balance*(1+intRate)
print("Balance after 4 year: $%.2f"%balance)
#Calculate the balance after 5 year
balance=balance*(1+intRate)
print("Balance after 5 year: $%.2f"%balance)
#Calculate the balance after 6 year
balance=balance*(1+intRate)
print("Balance after 6 year: $%.2f"%balance)
#Calculate the balance after 7 year
balance=balance*(1+intRate)
print("Balance after 7 year: $%.2f"%balance)
#Calculate the balance for 8 year
balance=balance*(1+intRate)
print("Balance after 8 year: $%.2f"%balance)
#Calculate the balance for 9 year
balance=balance*(1+intRate)
print("Balance after 9 year: $%.2f"%balance)
#Calculate the balance for 10 year
balance=balance*(1+intRate)
print("Balance after 10 years: $%.2f"%balance)