home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume29 / loancalc / part01 < prev    next >
Encoding:
Text File  |  1992-05-12  |  7.9 KB  |  289 lines

  1. Newsgroups: comp.sources.misc
  2. From: sjk@netcom.com (Shel Kaphan)
  3. Subject:  v29i095:  loancalc - a loan calculator, Part01/01
  4. Message-ID: <1992May11.161233.7400@sparky.imd.sterling.com>
  5. X-Md4-Signature: ceceddd4cc3fe1f1be64a5628ccfc2b5
  6. Date: Mon, 11 May 1992 16:12:33 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: sjk@netcom.com (Shel Kaphan)
  10. Posting-number: Volume 29, Issue 95
  11. Archive-name: loancalc/part01
  12. Environment: UNIX
  13.  
  14. #! /bin/sh
  15. # This is a shell archive.  Remove anything before this line, then feed it
  16. # into a shell via "sh file" or similar.  To overwrite existing files,
  17. # type "sh file -c".
  18. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  19. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  20. # Contents:  loan.c makefile
  21. # Wrapped by kent@sparky on Mon May 11 11:06:50 1992
  22. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  23. echo If this archive is complete, you will see the following message:
  24. echo '          "shar: End of archive."'
  25. if test -f 'loan.c' -a "${1}" != "-c" ; then 
  26.   echo shar: Will not clobber existing file \"'loan.c'\"
  27. else
  28.   echo shar: Extracting \"'loan.c'\" \(6047 characters\)
  29.   sed "s/^X//" >'loan.c' <<'END_OF_FILE'
  30. X/*
  31. X  loan.c
  32. X
  33. X  Copyright (C) 1990 by Shel Kaphan    (sjk@netcom.com, sjk@well.sf.ca.us).
  34. X
  35. X  This program is distributed in the hope that it will be useful,
  36. X  but WITHOUT ANY WARRANTY.  The author accepts no responsibility to anyone
  37. X  for any consequences of using it or for whether it serves any particular 
  38. X  purpose or works at all.
  39. X
  40. X  This file may be freely copied, modified, and redistributed,
  41. X  but only so long as this notice is preserved on all copies.
  42. X
  43. X  May be compiled and linked with:
  44. X     gcc -o loan loan.c -lm
  45. X
  46. X*/
  47. X
  48. X
  49. X#include <ctype.h>
  50. X#include <math.h>
  51. X#include <stdio.h>
  52. X
  53. Xint payments_per_year=12;
  54. X
  55. Xfloat expt (num, pow)
  56. X     float num;
  57. X     int pow;
  58. X{
  59. X  float result=1.0;
  60. X  int i;
  61. X  for(i=0; i<pow; i++, result *= num);
  62. X  return result;
  63. X}
  64. X
  65. X
  66. Xvoid show_params(principal, interest_rate, monthly_payment, number_of_payments)
  67. X     float principal;
  68. X     float interest_rate;
  69. X     float monthly_payment;
  70. X     int number_of_payments;
  71. X{
  72. X  float total_interest(float,float,float,int);
  73. X  printf("(P)rincipal          %9.2f\n", principal);
  74. X  printf("(M)onthly payment    %9.2f\n", monthly_payment);
  75. X  printf("(I)nterest rate      %10.3f%%\n", interest_rate*100);
  76. X  printf("(N)umber of payments %6d\n", number_of_payments);
  77. X  printf("\nTotal Interest       %9.2f\n", total_interest(principal,interest_rate,monthly_payment,
  78. X                                                          number_of_payments) );
  79. X
  80. X
  81. X}
  82. X
  83. X
  84. Xfloat total_interest(float principal,float interest_rate,float monthly_payment, int num_payments)
  85. X{ 
  86. X     int m;
  87. X     float total=0.0;
  88. X     float remaining_principal = principal;
  89. X     for (m=0; m<num_payments; m++) {
  90. X         float monthly_interest = (remaining_principal * interest_rate) / 12;
  91. X         remaining_principal -= (monthly_payment - monthly_interest);
  92. X         total += monthly_interest;
  93. X    }
  94. X    return total;
  95. X}
  96. X
  97. X
  98. X
  99. Xfloat monthly_payment(principal, interest_rate, num_payments)
  100. X     float principal;
  101. X     float interest_rate;
  102. X     int num_payments;
  103. X{
  104. X
  105. X  float monthly_rate = interest_rate / payments_per_year;
  106. X  float monthly_interest = principal * monthly_rate;
  107. X  float compounding_factor = pow((double)(monthly_rate+1.0),
  108. X                 (double)num_payments);
  109. X  float monthly_excess = monthly_interest/(compounding_factor - 1.0);
  110. X  float monthly_payment = monthly_interest+monthly_excess;
  111. X  return monthly_payment;
  112. X          
  113. X}
  114. X
  115. Xfloat loan_size(monthly_payment, interest_rate, num_payments)
  116. X     float monthly_payment;
  117. X     float interest_rate;
  118. X     int num_payments;
  119. X{
  120. X  float monthly_rate = interest_rate / payments_per_year;
  121. X  float compounding_factor = pow((double)(monthly_rate+1.0),
  122. X                 (double)num_payments);
  123. X  float monthly_excess = monthly_payment/compounding_factor;
  124. X  float monthly_interest = monthly_payment - monthly_excess;
  125. X  float principal = monthly_interest/monthly_rate;
  126. X  return (principal);
  127. X}
  128. X
  129. Xfloat interest_rate(principal, mon_payment, num_payments)
  130. X     float principal;
  131. X     float mon_payment;
  132. X     int num_payments;
  133. X{
  134. X
  135. X  float guess = (payments_per_year * mon_payment)/principal;
  136. X  float inc = guess * -0.1;
  137. X  float mp;
  138. X  int i;
  139. X  for(i=0; i<1000; i++) {
  140. X    mp = monthly_payment(principal, guess, num_payments);
  141. X/*    printf("guess = %f    estimate = %.2f\n", guess, mp); */
  142. X    if (fabs(mp - mon_payment) <= .005) return guess;
  143. X    if (mp-mon_payment > 0) {
  144. X      if (inc > 0) inc = inc * -0.5;
  145. X    } else {
  146. X      if (inc < 0) inc = inc * -0.5;
  147. X    }
  148. X    guess += inc;
  149. X
  150. X    if (guess <=0) {
  151. X      guess -= inc;
  152. X      guess *= .05;
  153. X    }
  154. X
  155. X  }
  156. X
  157. X  printf("\n*** This is getting nowhere! Try something more realistic... ***\n\n");
  158. X
  159. X  return 0.0;
  160. X}
  161. X
  162. X
  163. Xint number_of_payments(principal, monthly_payment, interest_rate)
  164. X     float principal;
  165. X     float monthly_payment;
  166. X     float interest_rate;
  167. X{
  168. X  float compounding_factor;
  169. X  int num_payments;
  170. X
  171. X  float monthly_rate = interest_rate / payments_per_year;
  172. X  float monthly_interest = monthly_rate * principal;
  173. X  float monthly_excess = monthly_payment - monthly_interest;
  174. X  if (monthly_excess<=0.0) {
  175. X    printf("\n*** THIS LOAN CAN NEVER BE PAID OFF AT THIS INTEREST RATE ***\n\n");
  176. X    return 0;
  177. X  }
  178. X  compounding_factor = monthly_payment / monthly_excess;
  179. X  num_payments = (int) ceil(log((double)compounding_factor) /
  180. X               log((double)(1.0 + monthly_rate)));
  181. X  return num_payments;
  182. X}
  183. X
  184. X
  185. Xvoid command_loop() {
  186. X  char command;
  187. X  float arg1, arg2, arg3;
  188. X  int matches;
  189. X  float int_rate = .10;
  190. X  float principal = 100000;
  191. X  int num_payments = 360;
  192. X  float mon_payment = 877.58;
  193. X  char cbuf[100];
  194. X
  195. X  printf("Type '?' for help\n");
  196. X  while(1) {
  197. X    printf("> ");
  198. X    if (gets(cbuf)==NULL) return;
  199. X    matches = sscanf(cbuf, " %c %f %f %f ", &command, &arg1, &arg2, &arg3);
  200. X    if (matches<=0) continue;
  201. X
  202. X    if (isupper(command)) command = tolower(command);
  203. X
  204. X    switch(command) {
  205. X    case 'i':            /* set interest rate */
  206. X      if (matches>=2) {
  207. X    int_rate = arg1;
  208. X    if (int_rate >= 1.0) int_rate *= .01;
  209. X      } else
  210. X    int_rate = interest_rate(principal, mon_payment, num_payments);
  211. X
  212. X      break;
  213. X    case 'p':            /* set principal */
  214. X      if (matches>=2) {
  215. X    principal = arg1;
  216. X      } else
  217. X    principal = loan_size(mon_payment, int_rate, num_payments);
  218. X      break;
  219. X    case 'n':            /* set number of payments */
  220. X      if (matches>=2) {
  221. X    num_payments = (int)ceil((double)arg1);
  222. X      } else {
  223. X    int n = number_of_payments(principal, mon_payment, int_rate);
  224. X    if (n)
  225. X      num_payments=n;
  226. X    else
  227. X      matches=0;
  228. X      }
  229. X      break;
  230. X    case 'm':            /* set monthly payment */
  231. X      if (matches>=2) {
  232. X    mon_payment = arg1;
  233. X      }  else
  234. X    mon_payment = monthly_payment(principal, int_rate, num_payments);
  235. X      break;
  236. X    case 'q':
  237. X      exit(0);
  238. X    case '?':
  239. X    case 'h':
  240. X      printf("Type 'p', 'm', 'i', or 'n' followed by a number to set the Principal,\n\
  241. XMonthly payment, Interest rate, or Number of payments, respectively.\n\
  242. XThe same commands with no arguments recalculate the specified quantity.\n\
  243. XType q to quit.\n\n");
  244. X      matches=0;
  245. X      break;
  246. X    default:
  247. X      printf("Huh?\n");
  248. X      matches=0;
  249. X      break;
  250. X    }
  251. X
  252. X    if (matches==1)
  253. X      show_params(principal, int_rate, mon_payment, num_payments);
  254. X
  255. X  }
  256. X
  257. X}
  258. X
  259. X
  260. X
  261. Xmain () {
  262. X  command_loop();
  263. X}
  264. X
  265. END_OF_FILE
  266.   if test 6047 -ne `wc -c <'loan.c'`; then
  267.     echo shar: \"'loan.c'\" unpacked with wrong size!
  268.   fi
  269.   # end of 'loan.c'
  270. fi
  271. if test -f 'makefile' -a "${1}" != "-c" ; then 
  272.   echo shar: Will not clobber existing file \"'makefile'\"
  273. else
  274.   echo shar: Extracting \"'makefile'\" \(40 characters\)
  275.   sed "s/^X//" >'makefile' <<'END_OF_FILE'
  276. XCC=gcc
  277. X
  278. Xloan:
  279. X    ${CC} -o loan loan.c -lm
  280. END_OF_FILE
  281.   if test 40 -ne `wc -c <'makefile'`; then
  282.     echo shar: \"'makefile'\" unpacked with wrong size!
  283.   fi
  284.   # end of 'makefile'
  285. fi
  286. echo shar: End of archive.
  287. exit 0
  288. exit 0 # Just in case...
  289.