At some point in time, everyone has to borrow some money to buy something. Often the biggest purchase to be made by anyone is buying a house. One of the ways of borrowing money is by means of a *fixed* rate mortgage, i.e. a fixed monthly payment is paid until the loan is paid off with interest at the end of its term.

`mp = rP(1+r)`^{n} / ((1+r)^{n}-1)
mp = monthly payment
r = monthly interest rate (The monthly rate is calculated by
dividing the yearly interest rate by 12. e.g. 5% = 0.05/12
= 0.0042)
n = number of monthly payments (loan term)
P = principal amount borrowed

The Fortran program shown below calculates the monthly payments for a range of yearly interest rates, X..Y, e.g. 2.0→5.0, in 0.5% increments. Half the program is take up with user input, the remainder prints out a table of monthly mortgage payments for each of the rates.

```
program mortgage
real :: p, mp, r, rlow, rhigh, incr, rate
integer :: nyrs, n
write(*,*) "mortgage principal: "
read(*,*) p
write(*,*) "low interest rate (%/year): "
read(*,*) rlow
write(*,*) "high interest rate (%/year): "
read(*,*) rhigh
write(*,*) "loan period (years): "
read(*,*) nyrs
n = nyrs * 12
incr = 0.5
write(*,*) 'Interest rate (%) Monthly payment($)'
rate = rlow
do while (rate <= rhigh)
r = (rate/100.0)/12.0
mp = p * r * ((1+r)**n)/((1+r)**n-1)
write(*,10) rate, mp
rate = rate + incr
end do
10 format(5x,f5.2,15x,f7.2)
end program mortgage
```

There isn’t anything vastly different in this program, except the `format`

statement on Line 23. This basically just formats the output in the `write`

statement on Line 20. It prints 5 spaces (5x), followed by a real number of length 5, with 2 decimal places (f5.2), followed by 15 spaces (15x), and finally a real number of length 7 with 2 decimal places (f7.2). This is shown visually below – the `X`

relates to spaces, the `f`

to real numbers (there are other codes for integers, and characters).

Here is a sample run of the program.

mortgage principal: 100000 lowest interest rate (%/year): 1.5 highest interest rate (%/year): 4.0 number of years: 25 Interest rate (%) Monthly payment($) 1.50 399.93 2.00 423.86 2.50 448.63 3.00 474.21 3.50 500.62 4.00 527.84

For comparison, here is the same program written in Python.

```
p = float(input('mortgage principal: '))
rlow = float(input('low interest rate (%/year): '))
rhigh = float(input('high interest rate (%/year): '))
nyrs = int(input('loan period (years): '))
n = nyrs * 12
incr = 0.5
print('Interest rate (%) Monthly payment($)')
rate = rlow
while (rate <= rhigh):
r = (rate/100.0)/12.0
mp = p * r * ((1+r)**n)/((1+r)**n-1)
print(" %5.2f %5.2f" % (rate,mp))
rate = rate + incr
```

The only real difference between the Python and Fortran programs is that Python compresses the part of the program dealing with input input one statement per input, as opposed to two. The other problem is that Python doesn’t really “feel” like a program, just a bunch of statements.