-
Notifications
You must be signed in to change notification settings - Fork 3
/
mortgage.cpp
174 lines (154 loc) · 5.54 KB
/
mortgage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//*****************************************************************
//*****************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//lint -esym(119, strtod) //Too many arguments (2) for prototype 'strtod(const char *)'
//lint -esym(818, argv) // Pointer parameter 'argv' (line 147) could be declared as pointing to const
typedef unsigned int uint ;
static double balance = 0.0 ;
static double i = 0.0 ;
static double N = 0.0 ;
static double overpay = 0.0 ;
static double r = 0.0 ;
//*****************************************************************
void usage(void)
{
puts("Usage: MORTGAGE principle interest number_months overpay");
puts("In normal operation, this program computes the monthly payment,") ;
puts("and payment summary table, for a loan with the input parameters.") ;
puts("") ;
puts("Alternate mode:") ;
puts("If number_months is 0, overpay is treated as the payment amount,") ;
puts("and this program will calculate and display the number of months") ;
puts("required to pay off principle.") ;
puts("") ;
puts("Also, if overpay is greater than payment, then overpay is treated as") ;
puts("total payment amount, and overpay is calculated from that.") ;
puts("") ;
}
//*****************************************************************
static void compute_mortgage_payment(void)
{
double payment, principle, interest;
double yprinciple, yinterest ;
double tprinciple, tinterest;
unsigned j, k ;
int ibalance;
// calculate standard payment
payment = (balance * r) / (1.0 - (1.0 / pow ((1.0 + r), N)));
if (overpay > payment) {
overpay = overpay - payment ;
}
// display input parameters
puts ("input values:");
printf ("Starting Principle: $%.0f, loan length: %.0f months\n", balance, N);
printf ("Interest rate: %.3f, overpay: $%.2f per month\n\n", i, overpay);
printf ("basic monthly payment = %.2f\n\n", payment);
// display amortization table
puts ("year interest principle balance");
puts ("==== ======== ========= =========");
tprinciple = tinterest = 0.0;
j = 1;
ibalance = (int) (balance * 100) ;
// while (balance > 0.00) {
while (ibalance > 0) {
// calculate one year of data, in monthly increments
yprinciple = 0;
yinterest = 0;
for (k = 0; k < 12; k++) {
interest = balance * r;
if (balance + interest > payment) {
principle = payment - interest + overpay;
balance -= principle;
yprinciple += principle;
yinterest += interest;
}
else {
yprinciple += balance;
yinterest += interest;
balance = 0.0;
break;
}
}
tprinciple += yprinciple;
tinterest += yinterest;
printf ("%02u: %9.2f %9.2f %9.2f\n", j, yinterest, yprinciple, balance);
ibalance = (int) (balance * 100) ;
j++;
}
// display totals paid
puts ("==== ========= ========= =========");
printf ("total: %9.2f %9.2f %9.2f\n",
tinterest, tprinciple, tprinciple + tinterest);
}
//*****************************************************************
static void compute_months_remaining(void)
{
double remaining_balance = balance ;
double payment = overpay ;
double tprinciple = 0.0, tinterest = 0.0;
double yprinciple = 0.0, yinterest = 0.0;
double principle ;
double interest = balance * r;
if (payment < interest) {
printf("Cannot pay off, interest (%.2f) is greater than payment (%.2f)\n", interest, payment) ;
return ;
}
printf("initial balance = %.2f, payment is %.2f\n", balance, payment) ;
puts("") ;
puts("year principle interest balance") ;
puts("===== ========= ========= =========");
uint month = 0 ;
uint year = 0 ;
bool done = false ;
while (remaining_balance > 0.01) { // 848.65
interest = remaining_balance * r; // 4.24
if (remaining_balance < payment) {
done = true ;
payment = remaining_balance + interest; // 848.65
principle = payment - interest ;
remaining_balance = 0.0 ;
} else {
principle = payment - interest ;
remaining_balance -= principle ;
}
tprinciple += principle ;
tinterest += interest ;
yprinciple += principle ;
yinterest += interest ;
if ((++month % 12) == 0 || done) {
printf("%3u %9.2f %9.2f %9.2f\n", ++year, yprinciple, yinterest, remaining_balance) ;
yprinciple = yinterest = 0.0 ;
}
}
// display totals paid
puts ("===== ========= ========= =========");
printf("total: %9.2f %9.2f %9.2f\n",
tprinciple, tinterest, tprinciple + tinterest);
printf("payoff in %u years, %u months\n", month / 12, month % 12) ;
}
//*****************************************************************
int main (int argc, char **argv)
{
// read command-line arguments
if (argc != 5) {
usage() ;
return 1;
}
balance = strtod (argv[1], NULL);
i = strtod (argv[2], NULL);
N = strtod (argv[3], NULL);
overpay = strtod (argv[4], NULL);
r = (i / 100.0) / 12.0 ; // assume interest compounded monthly
if (overpay < 0) {
usage() ;
return 1;
}
if (N > 1.0) {
compute_mortgage_payment() ;
} else {
compute_months_remaining() ;
}
return 0;
}