10 REM > Payment 0.10 06-Jan-2005
   20 REM Calculate mortgage repayment terms, etc
   30 REM J.G.Harston
   40 :
   50 REM Grrr. Can't get working - needs more research
   60 :
   70 VDU26,12
   80 PRINT "Mortgage Repayments Calculator"
   90 PRINT "=============================="'
  100 ON ERROR REPORT:IF ERR<>17:END
  110 REPEAT
  120   PRINT'"1: Loan table"'"2: Fixed repayment table"'"0: Exit  ";
  130   REPEATA$=GET$:UNTILINSTR("012",A$):PRINT
  140   IFA$="1":PROCLoanTable
  150   IFA$="2":PROCFixedRepaymentTable
  160 UNTILA$="0"
  170 END
  180 :
  190 :
  200 :
  210 :
  220 REM Display loan fixed repayment table
  230 REM ==================================
  240 DEFPROCFixedRepaymentTable
  250 INPUT "Loan amount: "loan
  260 INPUT "Base APR:    "apr
  270 INPUT "Initial monthly repayment:   "repayment
  280 INPUT "Annual percentage increment: "increment
  290 CLS
  300 PRINT"      Monthly    Interest   Amount"
  310 PRINT"Year  Payments   Charged    Outstanding"
  320 PRINT"                            at year end"
  330 :
  340 REM apr=apr/(1200):payment=loan*(apr/(1-(1+apr)^-(years*12)))            :REM Magic forumla
  350 :
  360 year=0                                  :REM No time passed yet
  370 REPEAT
  380   PRINT FNd(year+1,3);FNr(repayment,8,2);
  390   loan0=loan                              :REM Loan amount at start of year
  400   FOR month=1 TO 12                       :REM Twelve monthly repayments
  410     interest=(loan0*((1+apr/100)/12))/12
  420     loan=loan+interest                      :REM Add interest
  430     loan=loan-repayment                     :REM Deduct monthly repayment
  440   NEXT
  450   PRINT FNr(loan-loan0+12*repayment,8,2); :REM Interest charged
  460   PRINT FNr(loan,8,2)                     :REM Amount at end of year
  470   year=year+1                             :REM Another year gone
  480   repayment=repayment*(1+increment/100)   :REM Increase monthly repayment
  490 UNTIL loan<0                            :REM Loop until all loan paid off
  500 ENDPROC
  510 :
  520 :
  530 :
  540 :
  550 REM Display loan table of monthly repayments and terms
  560 REM ==================================================
  570 DEFPROCLoanTable
  580 INPUT "Loan amount: "loan
  590 INPUT "Base APR:    "apr
  600 inc0=0:inc1=2:incs=0.25:yr0=17:yr1=2
  610 CLS
  620 PRINTSPC6"Loan: "loan;SPC6"APR: ";apr;"%"'
  630 PRINTSPC6"Initial monthly payment with increment"
  640 PRINT"Term";
  650 FOR increment=inc0 TO inc1 STEP incs
  660   PRINT FNr(increment,5,2)"%";
  670 NEXT:PRINT
  680 FOR years=yr0 TO yr1 STEP -1:PRINT FNd(years,3);
  690     FOR increment=inc0 TO inc1 STEP incs
  700         payment=FNloan_payment(loan,apr,years,increment)
  710         PRINT FNr(payment,6,2);
  720     NEXT increment
  730     PRINT
  740 NEXT years
  750 ENDPROC
  760 :
  770 :
  780 REM Calculate monthly repayment to pay off loan
  790 REM ===========================================
  800 REM loan   = initial loan amount
  810 REM apr    = annual interest rate, applied monthly
  820 REM years  = maximum term to repay loan
  830 REM inc    = annual repayment increment
  840 REM
  850 REM Returns: monthly repayment to pay loan within term
  860 :
  870 DEFFNloan_payment(loan,apr,years,inc)
  880 LOCAL payment,term,step,dir,newdir
  890 payment=loan DIV (years*8)               :REM Initial guess at repayment
  900 step=payment DIV 2                       :REM Initial chop offset
  910 :
  920 REPEAT                                   :REM Trail increase repayment
  930     payment=payment+step                   :REM Increase repayment
  940     term=FNloan_term(loan,apr,payment,inc) :REM See what term this gives us
  950 UNTIL term<years                         :REM Loop until term less than required
  960 :
  970 dir=1                                    :REM Start by increasing repayment
  980 REPEAT
  990     payment=payment+step*dir               :REM Raise or lower payment by a single step
 1000     term=FNloan_term(loan,apr,payment,inc) :REM See what term this gives us
 1010     IF term>years THEN newdir=1 ELSE newdir=-1
 1020     IF newdir<>dir THEN step=step/2        :REM Reduce step when changing direction
 1030     dir=newdir
 1040 UNTIL term=years
 1050 :
 1060 REM apr=apr/(1200):payment=loan*(apr/(1-(1+apr)^-(years*12)))            :REM Magic forumla
 1070 :
 1080 =payment
 1090 :
 1100 :
 1110 :
 1120 :
 1130 REM Calculate loan term with fixed APR
 1140 REM ==================================
 1150 REM loan      = initial loan amount
 1160 REM apr       = annual interest rate, applied monthly
 1170 REM repayment = initial monthly repayment
 1180 REM increment = annual repayment increment
 1190 REM
 1200 REM Returns   : number of years to repay whole loan, rounded up
 1210 :
 1220 DEFFNloan_term(loan,apr,repayment,increment)
 1230 LOCAL year
 1240 year=0                                :REM No time passed yet
 1250 REPEAT
 1260   FOR month=1 TO 12                     :REM Twelve monthly repayments
 1270     loan=loan*((1+apr/100)^(1/12))    :REM Add interest - school taught me wrong formula!
 1280     REM loan=loan*(1+apr/1200)                :REM Add interest
 1290     loan=loan-repayment                   :REM Deduct monthly repayment
 1300   NEXT
 1310   year=year+1                           :REM Another year gone
 1320   repayment=repayment*(1+increment/100) :REM Increase monthly repayment
 1330 UNTIL loan<0                          :REM Loop until all loan paid off
 1340 =year
 1350 :
 1360 :
 1370 REM Number formatting functions
 1380 REM ===========================
 1390 DEFFNr(A,I%,D%):=FNd(INTA,I%)+"."+RIGHT$(STR$INT(A*10^D%),D%)
 1400 DEFFNd(A%,N%)=RIGHT$("         "+STR$A%,N%)
 1410 :
 1420 REM loan=loan*((1+apr/100)^(1/12))      :REM Add interest - school taught me wrong formula!