home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume29
/
loancalc
/
part01
/
loan.c
next >
Wrap
C/C++ Source or Header
|
1992-05-13
|
6KB
|
236 lines
/*
loan.c
Copyright (C) 1990 by Shel Kaphan (sjk@netcom.com, sjk@well.sf.ca.us).
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. The author accepts no responsibility to anyone
for any consequences of using it or for whether it serves any particular
purpose or works at all.
This file may be freely copied, modified, and redistributed,
but only so long as this notice is preserved on all copies.
May be compiled and linked with:
gcc -o loan loan.c -lm
*/
#include <ctype.h>
#include <math.h>
#include <stdio.h>
int payments_per_year=12;
float expt (num, pow)
float num;
int pow;
{
float result=1.0;
int i;
for(i=0; i<pow; i++, result *= num);
return result;
}
void show_params(principal, interest_rate, monthly_payment, number_of_payments)
float principal;
float interest_rate;
float monthly_payment;
int number_of_payments;
{
float total_interest(float,float,float,int);
printf("(P)rincipal %9.2f\n", principal);
printf("(M)onthly payment %9.2f\n", monthly_payment);
printf("(I)nterest rate %10.3f%%\n", interest_rate*100);
printf("(N)umber of payments %6d\n", number_of_payments);
printf("\nTotal Interest %9.2f\n", total_interest(principal,interest_rate,monthly_payment,
number_of_payments) );
}
float total_interest(float principal,float interest_rate,float monthly_payment, int num_payments)
{
int m;
float total=0.0;
float remaining_principal = principal;
for (m=0; m<num_payments; m++) {
float monthly_interest = (remaining_principal * interest_rate) / 12;
remaining_principal -= (monthly_payment - monthly_interest);
total += monthly_interest;
}
return total;
}
float monthly_payment(principal, interest_rate, num_payments)
float principal;
float interest_rate;
int num_payments;
{
float monthly_rate = interest_rate / payments_per_year;
float monthly_interest = principal * monthly_rate;
float compounding_factor = pow((double)(monthly_rate+1.0),
(double)num_payments);
float monthly_excess = monthly_interest/(compounding_factor - 1.0);
float monthly_payment = monthly_interest+monthly_excess;
return monthly_payment;
}
float loan_size(monthly_payment, interest_rate, num_payments)
float monthly_payment;
float interest_rate;
int num_payments;
{
float monthly_rate = interest_rate / payments_per_year;
float compounding_factor = pow((double)(monthly_rate+1.0),
(double)num_payments);
float monthly_excess = monthly_payment/compounding_factor;
float monthly_interest = monthly_payment - monthly_excess;
float principal = monthly_interest/monthly_rate;
return (principal);
}
float interest_rate(principal, mon_payment, num_payments)
float principal;
float mon_payment;
int num_payments;
{
float guess = (payments_per_year * mon_payment)/principal;
float inc = guess * -0.1;
float mp;
int i;
for(i=0; i<1000; i++) {
mp = monthly_payment(principal, guess, num_payments);
/* printf("guess = %f estimate = %.2f\n", guess, mp); */
if (fabs(mp - mon_payment) <= .005) return guess;
if (mp-mon_payment > 0) {
if (inc > 0) inc = inc * -0.5;
} else {
if (inc < 0) inc = inc * -0.5;
}
guess += inc;
if (guess <=0) {
guess -= inc;
guess *= .05;
}
}
printf("\n*** This is getting nowhere! Try something more realistic... ***\n\n");
return 0.0;
}
int number_of_payments(principal, monthly_payment, interest_rate)
float principal;
float monthly_payment;
float interest_rate;
{
float compounding_factor;
int num_payments;
float monthly_rate = interest_rate / payments_per_year;
float monthly_interest = monthly_rate * principal;
float monthly_excess = monthly_payment - monthly_interest;
if (monthly_excess<=0.0) {
printf("\n*** THIS LOAN CAN NEVER BE PAID OFF AT THIS INTEREST RATE ***\n\n");
return 0;
}
compounding_factor = monthly_payment / monthly_excess;
num_payments = (int) ceil(log((double)compounding_factor) /
log((double)(1.0 + monthly_rate)));
return num_payments;
}
void command_loop() {
char command;
float arg1, arg2, arg3;
int matches;
float int_rate = .10;
float principal = 100000;
int num_payments = 360;
float mon_payment = 877.58;
char cbuf[100];
printf("Type '?' for help\n");
while(1) {
printf("> ");
if (gets(cbuf)==NULL) return;
matches = sscanf(cbuf, " %c %f %f %f ", &command, &arg1, &arg2, &arg3);
if (matches<=0) continue;
if (isupper(command)) command = tolower(command);
switch(command) {
case 'i': /* set interest rate */
if (matches>=2) {
int_rate = arg1;
if (int_rate >= 1.0) int_rate *= .01;
} else
int_rate = interest_rate(principal, mon_payment, num_payments);
break;
case 'p': /* set principal */
if (matches>=2) {
principal = arg1;
} else
principal = loan_size(mon_payment, int_rate, num_payments);
break;
case 'n': /* set number of payments */
if (matches>=2) {
num_payments = (int)ceil((double)arg1);
} else {
int n = number_of_payments(principal, mon_payment, int_rate);
if (n)
num_payments=n;
else
matches=0;
}
break;
case 'm': /* set monthly payment */
if (matches>=2) {
mon_payment = arg1;
} else
mon_payment = monthly_payment(principal, int_rate, num_payments);
break;
case 'q':
exit(0);
case '?':
case 'h':
printf("Type 'p', 'm', 'i', or 'n' followed by a number to set the Principal,\n\
Monthly payment, Interest rate, or Number of payments, respectively.\n\
The same commands with no arguments recalculate the specified quantity.\n\
Type q to quit.\n\n");
matches=0;
break;
default:
printf("Huh?\n");
matches=0;
break;
}
if (matches==1)
show_params(principal, int_rate, mon_payment, num_payments);
}
}
main () {
command_loop();
}