loan amortization schedule

For students who are taking a programming course, may be restricted in what they can use, or have other unusual limitations on solving their task.

loan amortization schedule

Postby school19 » Thu Sep 15, 2016 1:17 am

Hello, everybody this is the assignment that I am having some trouble with

(Financial application: loan amortization schedule) The monthly payment for a given loan pays the principal and the interest. The monthly interest is computed by multiplying the monthly interest rate and the balance (the remaining principal). The principal paid for the month is therefore the monthly payment minus thenmonthly interest. Write a program that lets the user enter the loan amount, number of years, and interest rate, and then displays the amortization schedule for the loan.


The balance after the last payment may not be zero. If so, the last payment should be the normal monthly payment plus the final balance.


Write a loop to display the table. Since the monthly payment is the same for each month, it should be computed before the loop. The balance is initially the loan amount. For each iteration in the loop, compute the interest and principal and update the balance. The loop may look like this:

for i in range(1, numberOfYears * 12 + 1):

interest = monthlyInterestRate * balance

principal = monthlyPayment - interest

balance = balance - principal

print(i, "\t\t", interest, "\t\t", principal, "\t\t", balance)

This is the code that I have

Code: Select all
balance = int(input("Enter the loan amount: "))

years = int(input("Enter the number of years: "))
months = years * 12

interest = eval(input("Enter the interest rate: "))
monthlyinterest = interest /(100*12)

monthlypayment = balance * (monthlyinterest* (1 + monthlyinterest) ** months)/ ((1 + monthlyinterest)** months -1 )

totalpayment = monthlypayment * months

print('Monthly payment: ', format(monthlypayment,'.2f'))
print('Total amount of payments: ',  format(totalpayment,'.2f'))

print ('Payment#\t', 'Interest\t', 'Principal\t', 'Balance')
print ('---------------------------------------------------------')
for p in range(1, months+1):

    interest = monthlyinterest * balance
    principal = monthlypayment - interest
    balance -= principal
    print (p,'\t\t',format(interest,'.2f'), '\t\t', format(principal,'.2f'), '\t', format(balance,'.2f'))

The problem that I am having is that my final balance is 0.00 when it's suppose to be 0.01 it looks like there's an issue with the way the program is rounding my results but I am not sure how to fix it. I was hoping you guys could help me head in the right direction.
Last edited by micseydel on Thu Sep 15, 2016 1:58 am, edited 1 time in total.
Reason: Initial post lock.
Posts: 1
Joined: Thu Sep 15, 2016 1:13 am

Re: loan amortization schedule

Postby Ofnuts » Thu Sep 15, 2016 8:51 am

No, your final balance is indeed 0.00 or something very close...

Your problem is that even though you format things for display with two decimals, the program does all its computations with the full values. For instance,
with 10000/5/3.5 you show a payment of 181.92 but the code sees 181.91744970, and considers that all the payments by the customer will be 181.91744970.
So you have to explicitly round off the monthly payment to have no fractional cents, and use that number in your computations. Then you get a non-zero balance... which is going to be the fractional cents times the number of payments if you truncate the payment, and about half that if you round it.
This forum has been moved to See you there.
User avatar
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Return to Homework

Who is online

Users browsing this forum: No registered users and 4 guests