MODULE 5 · LOOPS AND PROGRAM STRUCTURE · 7/7

Module challenge: loan amortisation schedule

30 min60 XPExercise

Every bank, building society and car-finance company runs a batch job that produces amortisation schedules: a month-by-month breakdown of a loan showing how much of each payment is interest, and what is left to pay. Customers get it on their annual statement. Auditors ask for it. Mistakes of a single cent get noticed.

How a repayment loan works

Each month:

  1. The lender charges interest on the outstanding balance: interest = balance * annual rate / 12 / 100, rounded to the cent.
  2. The borrower pays a fixed amount.
  3. The new balance is balance + interest - payment.

The payment is normally fixed when the loan is set up. Because interest is charged on a shrinking balance, more of each payment goes towards paying off the loan as time goes on.

The last payment is almost never exactly the fixed amount. If the balance plus this month's interest is less than or equal to the usual payment, the borrower pays just that and the balance becomes zero.

           COMPUTE WS-INTEREST ROUNDED =
               WS-BALANCE * WS-ANNUAL-RATE / 1200

Dividing by 1200 does "divide by 12 months, then by 100 for percent" in one step. Doing both divisions in one COMPUTE means the intermediate result keeps its precision until the final rounding.

What this challenge uses

  • The batch skeleton: mainline, initialise, process, terminate.
  • PERFORM ... VARYING to number the months, with an UNTIL that tests the balance rather than the counter. VARYING does not require the condition to mention the counter.
  • A guard before the loop. If the payment does not even cover the first month's interest, the balance never goes down and the loop would never end. Real loan systems reject such a loan instead of running until the job is cancelled.

On the job

Financial programs are tested by reconciling totals: total paid must equal the principal plus total interest. When you change a calculation program, run the old and new versions against the same test data and check that these control totals still match.

Your task

Write the monthly schedule for a repayment loan. Read three lines:

  1. Principal (amount borrowed), e.g. 1000.00
  2. Annual interest rate in percent, e.g. 12.00
  3. Fixed monthly payment, e.g. 200.00

Each month:

  • interest = balance * rate / 1200, rounded to the cent
  • if balance + interest is less than or equal to the fixed payment, this month's payment is balance + interest (the final payment); otherwise it is the fixed payment
  • balance = balance + interest - payment

Stop when the balance reaches zero. Output:

MTH    PAYMENT   INTEREST    BALANCE
  1     200.00      10.00     810.00
  2     200.00       8.10     618.10
  3     200.00       6.18     424.28
  4     200.00       4.24     228.52
  5     200.00       2.29      30.81
  6      31.12       0.31       0.00
TOTAL PAID:         1031.12
TOTAL INTEREST:       31.12

Guard: if the fixed payment is less than or equal to the first month's interest, display only PAYMENT TOO LOW and stop.

The starter reads the input and provides 2100-SHOW-MONTH, which displays one detail line. The totals use WS-TOTAL-OUT (PIC Z(7)9.99). Number the months with PERFORM ... VARYING WS-MONTH, and use no GO TO.

fixed format
Run your program to see its output here. The first visible test's input and datasets are used.
Submit to grade your program against every test.