-
Notifications
You must be signed in to change notification settings - Fork 0
/
loan.c
308 lines (264 loc) · 8.92 KB
/
loan.c
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
loan
Steve Connet
Determines your monthly payment of a simple loan.
compile with:
cc -O2 -o loan loan.c -lm
The following functions are supported:
1. calculate payment given interest and period
2. calculate payment given interest
3. calculate payment given period
4. calculate payment, period, and interest
5. calculate principle given period and interest
6. calculate principle and interest given period
7. calculate principle and period given interest
8. calculate principle, period, and interest
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // getopt
#define SHOW_DEFAULT 0x00
#define SHOW_PERIOD 0x01
#define SHOW_RATE 0x02
void usage()
{
printf("\nUsage: loan -p principle [-i interest_rate | -t loan_period]"
"\n loan -m payment [-i interest_rate | -t loan_period]"
"\nExample: loan -i 7.0 -p 39000.00 -t 60.0\n\n"
"-i simple yearly interest rate\n"
"-p principle amount of loan\n"
"-t loan period in months (ie. number of payments)\n"
"-m monthly payment\n"
"-h help I don't understand\n\n"
"ordering of arguments does not matter\n"
"unspecified arguments will be solved if possible\n"
"Report bugs to <[email protected]>\n\n");
}
void help()
{
printf("Definitions:\n"
"Break Even Years = number of years to pay off principle if"
" payment went to principle alone.\n"
"Interest%% = Total interest paid as a percentage of Principal.\n");
}
// ----------------------------------------------------------------------------
// calculate monthly payment given interest and period
void calcPayment(double principleAmount, double yearlyInterestRate,
double numberPayments, int options)
{
double monthlyInterestRate = yearlyInterestRate / 1200.0;
double x = pow(1 + monthlyInterestRate, -numberPayments);
double monthlyPayment = principleAmount * monthlyInterestRate / (1 - x);
double totalPaid = monthlyPayment * numberPayments;
double interestPaid = totalPaid - principleAmount;
double interestPaidPercent = (interestPaid / principleAmount) * 100.0;
double breakEvenYears = (principleAmount / monthlyPayment) / 12.0;
printf("Monthly: %-12.2f\t", monthlyPayment);
if(options & SHOW_PERIOD)
{
printf("Num Payments: %-12.2f\t", numberPayments);
}
if(options & SHOW_RATE)
{
printf("Rate: %-12.2f\t", yearlyInterestRate);
}
printf("Interest: %-12.2f\tTotal: %-12.2f\tInterest%%: %-12.2f"
"\tBreakeven: %-12.2f\n",
interestPaid, totalPaid, interestPaidPercent, breakEvenYears);
}
// calculate monthly payment given interest
void calcPaymentAndPeriod(double principleAmount, double yearlyInterestRate)
{
double numberPayments = 12;
while(numberPayments <= 360)
{
calcPayment(principleAmount, yearlyInterestRate, numberPayments,
SHOW_PERIOD);
numberPayments += 12.0;
}
}
// calculate monthly payment given period
void calcPaymentAndInterest(double principleAmount, double numberPayments)
{
double interestRate = 1.0;
while(interestRate < 26.0)
{
calcPayment(principleAmount, interestRate, numberPayments,
SHOW_RATE);
interestRate += 1.0;
}
}
// calculate payment, period, and interest
void calcPaymentPeriodAndInterest(double principleAmount)
{
double numberPayments = 12;
while(numberPayments <= 360)
{
printf("Num Payments: %-12.2f\n", numberPayments);
calcPaymentAndInterest(principleAmount, numberPayments);
numberPayments += 12.0;
printf("\n");
}
}
// ----------------------------------------------------------------------------
// calculate principle given period and interest
void calcPrinciple(double monthlyPayment, double numberPayments,
double yearlyInterestRate, int options)
{
double monthlyInterestRate = yearlyInterestRate / 1200.0;
double x = pow(1 + monthlyInterestRate, -numberPayments);
double principleAmount = monthlyPayment * (1 - x) / monthlyInterestRate;
double totalPaid = monthlyPayment * numberPayments;
double interestPaid = totalPaid - principleAmount;
double interestPaidPercent = (interestPaid / principleAmount) * 100.0;
double breakEvenYears = (principleAmount / monthlyPayment) / 12.0;
printf("Principle: %-12.2f\t", principleAmount);
if(options & SHOW_PERIOD)
{
printf("Num Payments: %-12.2f\t", numberPayments);
}
if(options & SHOW_RATE)
{
printf("Rate: %-12.2f\t", yearlyInterestRate);
}
printf("Interest: %-12.2f\tTotal: %-12.2f\tInterest%%: %-12.2f\t"
"Breakeven: %-12.2f\n",
interestPaid, totalPaid, interestPaidPercent, breakEvenYears);
}
// calculate principle and interest given period
void calcPrincipleAndInterest(double monthlyPayment, double numberPayments)
{
double interestRate = 1.0;
while(interestRate < 25.0)
{
calcPrinciple(monthlyPayment, numberPayments, interestRate,
SHOW_RATE);
interestRate += 1.0;
}
}
// calculate principle and period given interest
void calcPrincipleAndPeriod(double monthlyPayment, double yearlyInterestRate)
{
double numberPayments = 12;
while(numberPayments <= 360)
{
calcPrinciple(monthlyPayment, numberPayments, yearlyInterestRate,
SHOW_PERIOD);
numberPayments += 12.0;
}
}
// calculate principle, period, and interest
void calcPrinciplePeriodAndInterest(double monthlyPayment)
{
double numberPayments = 12;
while(numberPayments <= 360)
{
printf("Num Payments: %-12.2f\n", numberPayments);
calcPrincipleAndInterest(monthlyPayment, numberPayments);
numberPayments += 12.0;
printf("\n");
}
}
// ----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
double principleAmount = -999;
double monthlyPayment = -999;
double yearlyInterestRate = -999;
double numberPayments = -999;
int retval = EXIT_FAILURE;
int c;
while((c = getopt(argc, argv, "h:i:p:t:m:")) != -1)
{
switch(c)
{
case 'h':
help();
break;
case 'i':
yearlyInterestRate = strtod(optarg, NULL);
break;
case 'p':
principleAmount = strtod(optarg, NULL);
break;
case 't':
numberPayments = strtod(optarg, NULL);
break;
case 'm':
monthlyPayment = strtod(optarg, NULL);
break;
default:
usage();
break;
}
}
// printf("yearlyInterestRate: %-12.2f\n", yearlyInterestRate);
// printf("principleAmount: %-12.2f\n", principleAmount);
// printf("numberPayments: %-12.2f\n", numberPayments);
// printf("monthlyPayment: %-12.2f\n", monthlyPayment);
// invalid, must have at least principle or payment
if(principleAmount < 0 && monthlyPayment < 0)
{
usage();
}
else if(principleAmount > 0 && monthlyPayment > 0)
{
usage();
printf("Cannot specify BOTH -m and -p arguments at the same time\n");
}
// (-m) solve for principle amount
else if(monthlyPayment > 0)
{
retval = EXIT_SUCCESS;
if(numberPayments > 0 && yearlyInterestRate > 0)
{
// calculate principle given period and interest
calcPrinciple(monthlyPayment, numberPayments, yearlyInterestRate,
SHOW_DEFAULT);
}
else if(yearlyInterestRate > 0)
{
// calculate principle and period given interest
calcPrincipleAndPeriod(monthlyPayment, yearlyInterestRate);
}
else if(numberPayments > 0)
{
// calculate principle and interest given period
calcPrincipleAndInterest(monthlyPayment, numberPayments);
}
else
{
// calculate principle, period, and interest
calcPrinciplePeriodAndInterest(monthlyPayment);
}
}
// (-p) solve for monthly payment
else if(principleAmount > 0)
{
retval = EXIT_SUCCESS;
if(numberPayments > 0 && yearlyInterestRate > 0)
{
calcPayment(principleAmount, yearlyInterestRate, numberPayments,
SHOW_DEFAULT);
}
else if(yearlyInterestRate > 0)
{
calcPaymentAndPeriod(principleAmount, yearlyInterestRate);
}
else if(numberPayments > 0)
{
calcPaymentAndInterest(principleAmount, numberPayments);
}
else
{
calcPaymentPeriodAndInterest(principleAmount);
}
}
else
{
help();
}
return retval;
}