Buying a house is one of the biggest financial transactions that many people ever undertake. Even a small difference in interest rates or monthly payments can make a large difference in how much you ultimately pay for your mortgage. However, these details are often buried in masses of paperwork--so it pays to be able to calculate the long-term cost of a mortgage yourself.

In a mortgage, the bank lends you a certain amount of principal to purchase a house at a certain interest rate. Every month, the amount you owe (the balance) first increases due to interest: 1/12 of the interest rate times the current balance. Then the balance decreases due to your monthly payment.

For example, suppose you borrow $100,000$100,000 at 5%5% annual interest, with $500$500 monthly payments. In the first month, the interest increases the balance by $416.67$416.67, and then your payment reduces it by $500$500, for a remaining balance of $99,916.67$99,916.67. In the second month, the interest charge is $416.32$416.32, and the remaining balance is $99,832.99$99,832.99.

If you continue this process, you get an amortization table like this:

month payment interest balance

1 500 416.67 99916.67

2 500 416.32 99832.99

3 500 415.97 99748.96

4 500 415.62 99664.58

5 500 415.27 99579.85

6 500 414.92 99494.76

7 500 414.56 99409.32

8 500 414.21 99323.53

9 500 413.85 99237.38

10 500 413.49 99150.87

You can also compute the total amount of time and money to pay off the mortgage. In this example, it takes 35 years and 11 months, and the total amount paid is approximately $215,458.84$215,458.84.

🎯 Create a mortgage calculator that takes as input the principal loan amount, interest rate, and monthly payment. As output, your calculator should generate an amortization table, and compute how many years and months it took to pay off the mortgage, and report the total amount of payments over that time.

Things to consider when implementing the calculator

- For dollar values, only display two digits of precision after the decimal point.

- You can assume that the bank will not round internally!
- You must not round internally either!)

- If this happens, the program should stop and display an appropriate error message.
- Manually stopping your code in this case is not considered a solution -- your submitted code must detect the problem itself, and not enter an infinite loop.

Problem 2(b). Testing.

When writing complicated code, it is always a good idea to test your code on simple examples.

🎯 Use the following test cases to make sure your code matches calculations you can perform by hand. (Of course, if it does not match, please fix your code.)🎯 For each test case, print the results.

For each, use $500$500 for the principal, and an annual interest rate of 5%5%.

- Monthly Payment: $100$100 (Total paid should be $506.35$506.35.)
- Monthly Payment: $500$500 (Total paid should be $502.09$502.09.)
- Monthly Payment: $1$1 (Code should display a suitable error message and quit gracefully.)

Problem 2(c). Calculator Analysis.

🎯 Answer the following questions in a markdown cell.

- Describe in your own words how the mortgage calculator works. What are the steps you are computing? What are you keeping track of in each iteration?
- Suppose you had a mortgage with a principal of $250,000$250,000, an interest rate of 4%4%, and a monthly payment of $1000$1000.

- How long would it take you to pay it off?
- How much would you have paid in total?
- Print the amortization schedule.