In: Computer Science
To model repayment of a loan, you need to calculate the interest on the balance and deduct any payments made. So if the balance at the start of the period is L the balance at the end of the period before the payment is L(1+r) where r is the interest rate per period (e.g. if the payments are monthly, it is a monthly interest rate) and the balance after the payment is L(1+r)−P where P is the payment per period (e.g. monthly payment). That is the starting balance for the next period which uses the same formula for the next payment. Create a Python program that will allow a user to calculate the amount of a loan after a specified number of monthly payments. Specifically, the program should:
Sample outputs:
Program screenshot:
Program code to copy:
# Taking input from user
L_value = float(input("Enter the initial value of the loan:
"))
i_rate = float(input("Enter the interest rate per month: "))
n_mo = float(input("Enter the number of months to calculate:
"))
m_pay = float(input("Enter the monthly payment: "))
# Calculate the total current value of the loan for a
month
cl_val = (L_value * (1 + i_rate/100)) - m_pay
print("")
cur_month = 1 # variable to keep track of current month
# Repeat the calculation for each month specified (n_mo) OR until
the
# loan is paid off (cl_val is less than or equal to 0)
while cur_month <= n_mo and cl_val > 0:
print("Month number {}: Total loan amount =
{:.2f}".format(cur_month,cl_val))
cur_month += 1
L_value = cl_val
# calculating new loan value
cl_val = (L_value * (1 + i_rate/100)) - m_pay