1+ /*
2+ Mortgage Calculator
3+ Calculates mortgage payments, amortization schedule, and plots balance over time .
4+ Assumes the mortgage compounds monthly.
5+ */
6+
7+ Option explicit
8+
9+ Dim float p ' principle
10+ Dim float r ' interest rate
11+ Dim float m ' monthly payment
12+ Dim integer payoff ' time till paid off
13+ Dim integer yrs ' years
14+ Dim integer n ' number of periods
15+
16+ start:
17+ CLS RGB (black )
18+ Print " Mortgage Calculator"
19+ Print " -------------------"
20+ Input " Loan Amount : $" , p
21+ Input " Int. Rate (APR): %" , r
22+ Input " Number of Years: " , yrs
23+
24+ If r <0 Or r >100 Then
25+ Print " Interest rate must be 0%<r<100%"
26+ GoTo start
27+ End If
28+
29+ n = 12 *yrs
30+ r = r /100 /12
31+ m = payment (p ,r ,n )
32+ payoff = n
33+
34+ Dim float bal (n ) ' remaining balance
35+ Dim float itr (n ) ' interest'
36+ Dim float pmts (n ) ' payments
37+ Array Set m, pmts ()
38+ amort
39+ balplot
40+
41+ Dim integer exit_flag =0
42+ On key opts
43+ Do While exit_flag =0
44+ Pause 100
45+ Loop
46+
47+ End
48+
49+ ' functions
50+ Function payment (p0,r,n ) As float
51+ payment = (p0 *r )/(1 -(1 +r )^-n )
52+ End Function
53+
54+ Function balance (p0,r,i,n ) As float
55+ Local float a, c
56+ a = (1 +r )^i
57+ c = payment (p0 ,r ,n )
58+ balance = p0 *a - c *(a -1 )/r
59+ End Function
60+
61+ ' subroutines
62+ Sub addpmt
63+ Local float apmt
64+ Local integer mnth
65+ Input " Add Payment:$" ,apmt
66+ Input " Pay Period : " ,mnth
67+ If mnth >=0 And mnth <=n Then
68+ pmts (mnth ) = pmts (mnth ) + apmt
69+ Else
70+ Print " invalid pay period"
71+ End If
72+ End Sub
73+
74+ Sub modpmt
75+ Local float cpmt
76+ Local integer start,stop ,i
77+ Input " Modified Payment:$" ,cpmt
78+ Input " Pay Period Start: " ,start
79+ Input " Pay Period Stop : " ,stop
80+ If start >=0 And stop <=n Then
81+ For i =start To stop
82+ pmts (i ) = cpmt
83+ Next i
84+ Else
85+ Print " invalid pay periods"
86+ End If
87+ End Sub
88+
89+ Sub amort
90+ Local integer i
91+ Local float it
92+ Local float newbal
93+ bal (0 ) = p
94+ itr (0 ) = 0
95+ pmts (0 ) = 0
96+ For i =1 To n
97+ it = bal (i -1 )*r
98+ newbal = bal (i -1 ) + it - pmts (i )
99+ itr (i ) = it
100+ If newbal > 0 Then
101+ bal (i ) = newbal
102+ Else
103+ ' loan paid off
104+ payoff = i
105+ bal (i ) = 0
106+ pmts (i ) = bal (i -1 ) + it
107+ Local integer j
108+ For j =i +1 To n
109+ bal (j ) = 0
110+ itr (j ) = 0
111+ pmts (j ) = 0
112+ Next j
113+ Exit For
114+ End If
115+ Next i
116+ End Sub
117+
118+ Sub printsum nbr
119+ Local string l1$,l2 $,l3 $,l4 $,l5 $,l6 $, fmt $
120+ l1$ = " Initial Loan : "
121+ l2$ = " Monthly Payment: "
122+ l3$ = " Total Paid : "
123+ l4$ = " Interest Paid : "
124+ l5$ = " Loan Repayment : "
125+ l6$ = " Interest Saved : "
126+ fmt$ = " $%10.2f"
127+
128+ Cat l1$,Format $(p ,fmt $)
129+ Cat l2$,Format $(m ,fmt $)
130+ Cat l3$,Format $(Math (sum pmts ()),fmt $)
131+
132+ Local float ipmt
133+ ipmt = Math (sum itr ())
134+ Cat l4$,Format $(ipmt , fmt $)
135+
136+ Local integer years, months
137+ years = payoff \12
138+ months = payoff - 12 *years
139+ Cat l5$,Format $(years ," %g years" )
140+ Cat l5$,Format $(months ," %g months" )
141+ Cat l6$,Format $(n *m -p -ipmt ,fmt $)
142+
143+ Print #nbr, l1 $
144+ Print #nbr, l2 $
145+ Print #nbr, l3 $
146+ Print #nbr, l4 $
147+ Print #nbr, l5 $
148+ Print #nbr, " ----------------------"
149+ Print #nbr, l6 $
150+
151+ End Sub
152+
153+ Sub balplot
154+ Local float x (n ),y (n ),yo (n )
155+ Local integer x1,x2 ,y1 ,y2
156+ x1 = 5 : x2 = MM.HRES -5
157+ y1 = 0 : y2 = MM.VRES \2
158+
159+ Local integer i
160+ For i =0 To n
161+ x (i ) = i
162+ yo (i ) = balance (p ,r ,i ,n )
163+ Next i
164+ Math window x (),x1 ,x2 ,x ()
165+ Math window yo (),y2 ,y1 ,yo ()
166+ Math window bal (),y2 ,y1 ,y ()
167+
168+ CLS RGB (black )
169+ Line graph x (),yo (),RGB (red )
170+ Line graph x (),y (),RGB (green )
171+ Line x1,y1 ,x1 ,y2 ,1 ,RGB (white )
172+ Line x1,y2 ,x2 ,y2 ,1 ,RGB (white )
173+
174+ Local integer xtks,xscl ,ytks ,yscl
175+ xtks = yrs
176+ xscl = (x2 -x1 )\xtks
177+ ytks = Fix (p /10000 )
178+ yscl = (y2 -y1 )\ytks
179+ For i =0 To ytks
180+ Pixel x1 -1 ,y1 +i *yscl,RGB (green )
181+ Next i
182+
183+ For i =0 To xtks
184+ Pixel x1 +i *xscl,y2 +1 ,RGB (green )
185+ Next i
186+
187+ Print @(0 ,y2 ) " "
188+ printsum 0
189+
190+ End Sub
191+
192+ Sub printfile
193+ Local string fname$
194+ Local string fmt$ = " $%8.2f "
195+ Line Input " Filename:" ,fname $
196+ Open fname$ For output As #1
197+ printsum 1
198+ Local string hd$
199+ hd$ = " Period "
200+ Cat hd$," Interest "
201+ Cat hd$," Payment "
202+ Cat hd$," Remaining "
203+ Print #1 ," "
204+ Print #1 ,hd $
205+ Print #1 ," ----------------------------------------------"
206+ Local integer i
207+ Local string l$
208+ For i =0 To n
209+ l$ = Format $(i ," %3.0f " )
210+ Cat l$,Format $(itr (i ),fmt $)
211+ Cat l$,Format $(pmts (i ),fmt $)
212+ Cat l$,Format $(bal (i ),fmt $)
213+ Print #1 ,l $
214+ Next i
215+ Close #1
216+ Print #0 ," Done."
217+ End Sub
218+
219+ Sub opts
220+ Local string k$
221+ k$ = Inkey $
222+ Select Case Asc (k $)
223+ Case 97 ' a
224+ addpmt
225+ amort
226+ CLS RGB (black )
227+ balplot
228+ Case 109 ' m
229+ modpmt
230+ amort
231+ CLS RGB (black )
232+ balplot
233+ Case 112 ' p
234+ printfile
235+ Case 114 ' r
236+ Array Set m, pmts ()
237+ amort
238+ CLS RGB (black )
239+ balplot
240+ Case 113 ' q
241+ exit_flag = 1
242+ Case 104 ' h
243+ Print " a) Additional payment"
244+ Print " m) Modify payment schedule"
245+ Print " p) Print result to text file"
246+ Print " r) Reset"
247+ Print " q) Quit"
248+ Print " h) Help"
249+ End Select
250+ End Sub
0 commit comments