## Loan Amortization

### May 12, 2009

My daughter, a freshman in high school, is just completing her first programming class, using Java. Her final assignment was to write a program to print a loan amortization table, given an initial balance, annual interest rate, term in months, and monthly payment.

Your task is to write that program (you are not restricted to Java), then print the amortization table for a three-year car loan of \$10,000 at 7% (it’s an old textbook). When you are finished, you are welcome to read a suggested solution, or to post your solution or discuss the exercise in the comments below.

Pages: 1 2

### 4 Responses to “Loan Amortization”

1. [...] Praxis – Loan Amortization By Remco Niemeijer Today’s Programming Praxis problem is about loan amortization. For those of you who, like me, had no idea [...]

2. Remco Niemeijer said

```import Text.Printf

amortize :: Float -> Float -> Int -> [(Int, Float, Float, Float)]
amortize _ _ 0 = []
amortize b r l = (l - 1, prn, int, b') : amortize b' r (l - 1)
where int = b * r
prn = int / (1 - (1 + r) ^^ (-l)) - int
b' = b - prn

amortization :: Float -> Float -> Int -> IO ()
amortization b r l = do
putStrLn "Left Principal Interest   Balance"
mapM_ table \$ (l, 0, 0, b) : amortize b (r / 12 / 100) l
where table (m, p, i, t) = printf "%4d %9.2f %8.2f %9.2f\n" m p i t

main :: IO ()
main = amortization 10000 7 36
```
3. grettke said

A Solution in PLT Scheme.

This is the first Scheme program I had ever wrote; and its present form is the result of many kind folks helping me out :)

```#lang scheme

(define mamort
(lambda (bal pcnt pay)
(format "Payment #,Payment Amount,Interest Paid,Principle
Paid,Balance,Total Interest~%")
(amort bal pcnt pay 12)))

(define amort
(lambda (bal pcnt pay freq)
(let loop ((bal bal) (tot-int 0) (pmt 1))
(let* ((int (* pcnt bal (/ freq)))
(prin (min (- pay int) bal))
(new-bal (- bal prin))
(new-int (+ int tot-int))
(done (< (+ int prin) pay)))
(if done
(printf "~s,~s,~s,~s,~s,~s~n"
pmt (+ int prin) int prin new-bal new-int)
(begin
(printf "~s,~s,~s,~s,~s,~s~n"
pmt pay int prin new-bal new-int)
(loop new-bal new-int (+ 1 pmt))))))))

(mamort 10000 .12 350)
```
4. Kathryn Bewig said

This is my favorite posting yet, Dad. :)