home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / utils / financil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  9.9 KB  |  448 lines

  1. From: Paul Pederson <decvax!ittvax!bunkerb!pop@Ucb-Vax.ARPA>
  2. To: net.sources
  3. Subject: Financial Helper
  4. Date: 16 Apr 85 21:19:14 GMT
  5.  
  6. This is a program which you may find useful for calculating different
  7. financial formulas.  Probably the most interesting of the calculations
  8. is the amortization schedule.
  9.  
  10. Compile as follows:
  11.  
  12.     cc -O -ltermcap -lm -o <outfile> <infile>
  13.  
  14.     where:
  15.  
  16.     <infile> is the name you have chosen for this source file
  17.  
  18.     <outfile> is the name of the executable file
  19.  
  20. P.S. Any fixes should be posted to the net.
  21.  
  22. ---------------------------cut here--------------------------------------
  23. /* This program is hereby given into the hands of the public, and is 
  24.  * considered to be public domain.  You may use it for whatever purposes
  25.  * you wish, including but not limited to: resale, personal use, teaching,
  26.  * incorporating into other programs, fun at parties.  The author, 
  27.  * regrettably, does not warrant it for any purpose whatsoever, so you're
  28.  * on your own.
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <sgtty.h>
  33. #include <math.h>
  34.  
  35. struct sgttyb otsgb;
  36. char mnths[12][4] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  37.  
  38. main()
  39. {
  40.     char choice;
  41.     
  42.     iniline();
  43.     clear_init();
  44.     resetline();
  45.  
  46.     while(1)
  47.     {
  48.         clear();
  49.         printf("\tFINANCIAL HELPER\n\n");
  50.         printf("0) Calculate Rate of Return\n");
  51.         printf("1) Interest Rate on Loan\n");
  52.         printf("2) Future Value of monthly deposits\n");
  53.         printf("3) Nominal & Effective Interest Rates\n");
  54.         printf("4) Compound Interest Rate\n");
  55.         printf("5) Simple Interest Rate\n");
  56.         printf("6) Future Value of Present Sum\n");
  57.         printf("7) Present Value of Future Sum\n");
  58.         printf("8) Amortization Schedule\n");
  59.         printf("9) Done\n");
  60.         printf("\nPlease choose one of the above ");
  61.         fflush(stdout);
  62.         iniline();
  63.         choice = getchar();
  64.         resetline();
  65.         clear();
  66.         switch (choice)
  67.         {
  68.             case '0': ROR(); break;
  69.             case '1': LOANINT(); break;
  70.             case '2': FVMP(); break;
  71.             case '3': NEIR(); break;
  72.             case '4': CIR(); break;
  73.             case '5': SIR(); break;
  74.             case '6': FVPS(); break;
  75.             case '7': PVFS(); break;
  76.             case '8': AMORT(); break;
  77.             case '9': printf("\n");exit(0);
  78.         }
  79.     }
  80. }
  81.  
  82. ROR()
  83. {
  84.     double presval,futval,earnings[100],temp0,temp1,temp2,temp3;
  85.     double finput();
  86.     int i,j, numpers, numyears, iinput();
  87.     char a;
  88.  
  89.     presval = finput("\tPurchase Price of investment ");
  90.     futval  = finput("\tFinal Sales price ");
  91.     numpers = iinput("\tTotal number of periods ");
  92.     numyears= iinput("\tNumber of periods in each year ");
  93.     for(i=0;i<numpers;i++)
  94.     {
  95.         printf("\tEarnings for period %d",i+1);
  96.         earnings[i] = finput(" ");
  97.     }
  98.     temp0 = 0.15 / numyears;
  99.     while(1)
  100.     {
  101.         temp1 = futval / pow((float)(1+ temp0), (float)numpers);
  102.         temp3 = temp2 = 0.0;
  103.         for (i=0;i<numpers;i++)
  104.         {
  105.             temp3 = earnings[i] / pow((float)(1+temp0),(float)i);
  106.             temp2+=temp3;
  107.         }
  108.         temp3 = temp0 * (temp1 + temp2) / presval;
  109.         if(temp0 - temp3 < 0.000001 && temp0 - temp3 > -0.000001)
  110.             break;
  111.         else
  112.             temp0 = temp3;
  113.     }
  114.     printf("\nRate of Return = %11.2f",temp0*numyears*100);
  115.     myret();
  116. }
  117.  
  118. double
  119. finput(string)
  120. char *string;
  121. {
  122.     char *temp, *buf, *malloc();
  123.     int i;
  124.  
  125.     printf("%s",string);
  126.     fflush(stdout);
  127.     buf = temp = malloc(20);
  128.     for(i=0; i<20;i++)
  129.     {
  130.         *buf = getchar();
  131.         if (*buf == '\n')
  132.             break;
  133.         else
  134.             buf++;
  135.     }
  136.     if(i == 20)
  137.         buf[-1] = '\0';
  138.     else
  139.         *buf = '\0';
  140.     return(atof(temp));
  141. }
  142.  
  143. iinput(string)
  144. char *string;
  145. {
  146.     char *temp, *buf, *malloc();
  147.     int i;
  148.  
  149.     printf("%s",string);
  150.     buf = temp = malloc(20);
  151.     for(i=0; i<20 && buf[-1] != '\n';i++)
  152.         *buf++ = getchar();
  153.     buf[-1] = '\0';
  154.     return(atoi(temp));
  155. }
  156.  
  157. LOANINT()
  158. {
  159.     double l, pv, p, l1;
  160.     int ny, n;
  161.     
  162.     pv = finput("\tPresent value of loan     ");
  163.     ny = iinput("\tNumber of terms per year  ");
  164.     n  = iinput("\tNumber of periods in loan ");
  165.     p  = finput("\tAmount of each payment    ");
  166.     l = 0.008;
  167.     while(1)
  168.     {
  169.         l1 = p / pv * pow((float)(1+l),(float)(n-1)) / pow((float)(1+l),(float)n);
  170.         if (l-l1 < 0.000001 && l-l1 > -0.000001)
  171.             break;
  172.         else
  173.             l = l1;
  174.     }
  175.     l = l1 * ny * 100;
  176.     printf("\tInterest rate on this loan is %11.4f %% \n",l);
  177.     myret();
  178. }
  179. FVMP()
  180. {
  181.     double rd, ir, t, i, fv;
  182.     int py, m, y;
  183.     
  184.     rd = finput("\tAmount of regular deposit   ");
  185.     py = iinput("\tNumber of deposits per year ");
  186.     m  = iinput("\tTotal number of months      ");
  187.     y = m / 12;
  188.     ir = finput("\tNominal interest rate       ");
  189.     i = ir / py / 100.0;
  190.     t = pow((float)(1+i),(float)(py*y));
  191.     t--;
  192.     t /= i;
  193.     fv = rd * t;
  194.     fprintf(stdout,"\tFuture value = $%11.2f \n",fv);
  195.     fflush(stdout);
  196.     myret();
  197. }
  198. NEIR()
  199. {
  200.     double fv, pv, nr, er;
  201.     int np, py;
  202.  
  203.     fv = finput("\tFuture value              $");
  204.     pv = finput("\tPresent value             $");
  205.     np = iinput("\tTotal number of periods    ");
  206.     py = iinput("\tNumber of periods per year ");
  207.     nr = (py * (pow((float)(fv / pv), (float)(1 / (float)np))) - py) *100.0;
  208.     er = (pow((float)(fv / pv) , (float)(py / (float)np)) - 1) * 100.0;
  209.     fprintf(stdout,"\tNominal rate = %8.4f %%\n",nr);
  210.     fprintf(stdout,"\tEffective rate = %8.4f %%\n",er);
  211.     fflush(stdout);
  212.     myret();
  213. }
  214. CIR()
  215. {
  216.     double fv, pv, i, pd;
  217.     int y, m, d, py;
  218.     
  219.     fv = finput("\tFuture value              $");
  220.     pv = finput("\tPresent value             $");
  221.     py = iinput("\tNumber of periods per year ");
  222.     y  = iinput("\tNumber of years            ");
  223.     m  = iinput("\tNumber of months           ");
  224.     d  = iinput("\tNumber of days             ");
  225.     pd = y*py+m*py/12+d*py/365;
  226.     i = (pow((float)(fv / pv), (float)(1.0/pd)) - 1) * 100 * py;
  227.     fprintf(stdout,"\tAnnual interest rate = %8.4f %%\n",i);
  228.     fflush(stdout);
  229.     myret();
  230. }
  231. SIR()
  232. {
  233.  
  234.     fprintf(stdout,"Not Currently available\n");
  235.     fflush(stdout);
  236.     myret();
  237. }
  238. FVPS()
  239. {
  240.     double p, r, i, t, a, s, f;
  241.     int m, n, k;
  242.  
  243.     p = finput("\tPresent sum                  $");
  244.     r = finput("\tAnnual interest rate          ");
  245.     m = iinput("\tNumber of periods per year    ");
  246.     n = iinput("\tNumber of periods to maturity ");
  247.     i = r / (float)m;
  248.     i /= 100.0;
  249.     t = i + 1.0;
  250.     a = t;
  251.     if (n != 1)
  252.         for(k = 1; k <= n-1; k++)
  253.         {
  254.             s = a * t;
  255.             a = s;
  256.         }
  257.     f = p * a;
  258.     fprintf(stdout,"\tFuture value = $%11.2f \n",f);
  259.     fflush(stdout);
  260.     myret();
  261. }
  262. PVFS()
  263. {
  264.     double f, r, i, t, a, s, p;
  265.     int m, n, x;
  266.  
  267.     f = finput("\tFuture sum                $");
  268.     r = finput("\tAnnual interest rate       ");
  269.     m = iinput("\tNumber of periods per year ");
  270.     n = iinput("\tTotal number of periods    ");
  271.     i = r / (float)m;
  272.     i /= 100.0;
  273.     t = 1.0 + i;
  274.     a = t;
  275.     if(n != 1)
  276.         for(x = 1; x <= n-1; x++);
  277.         {
  278.             s = a * t;
  279.             a = s;
  280.         }
  281.     p = f / a;
  282.     fprintf(stdout,"\tPresent value = $%11.2f\n",p);
  283.     fflush(stdout);
  284.     myret();
  285. }
  286. AMORT()
  287. {
  288.     double b,k,t,p,l,r,m,ay,at,a;
  289.     double finput();
  290.     int yr, c, L, z;
  291.     char chr, prtflg;
  292.     FILE *printer, *fopen(); 
  293.  
  294.     c = yr = 0;
  295.     p = finput("\tPrincipal Amount ");
  296.     L = iinput("\tNumber of months ");
  297.     r = finput("\tAnnual interest rate (ie 15.0 or 8.8) ");
  298.     m = finput("\tPayment, if known ");
  299.     while(c < 1 || c > 12)
  300.         c = iinput("\tPayment starts on month number ");
  301.     while(yr < 1 || yr > 3000)
  302.         yr = iinput("\tYear payments start ");
  303.     c--;
  304.     yr--;
  305.     fprintf(stdout,"Print schedule on printer? (y,n)");
  306.     fflush(stdout);
  307.     iniline();
  308.     prtflg = getchar();
  309.     resetline();
  310.     if(prtflg == 'Y' || prtflg == 'y')
  311.     {
  312.         printer = fopen("/dev/lp","w");
  313.         fflush(printer);
  314.     }
  315.     else
  316.         printer = stdout;
  317.     l = r / 1200.0;
  318.     t = 1.00 - (1.00 / pow((float)(1+l), (float)L));
  319.     k = p;
  320.     if (m == 0.0)
  321.         m = p * l / t;
  322.     amorthead(printer);
  323.     at = ay = 0.0;
  324.     for (z = 0; z < L; z++)
  325.     {
  326.         if (c >= 12)
  327.         {
  328.             yr++;
  329.             fprintf(printer,"\nYear of loan: %d\n",yr);
  330.             fprintf(printer,"\t$%11.2f for %d months at %11.2f %% \n",k,L,r);
  331.             at = at + ay;
  332.             fprintf(printer,"\tTotal interest paid during year --> $%11.2f\n\n",ay);
  333.             fflush(printer);
  334.             if (prtflg != 'Y' && prtflg != 'y')
  335.             {
  336.                 printf("\n\tPress return to continue, 'q' to end ");
  337.                 fflush(stdout);
  338.                 iniline();
  339.                 chr = getchar();
  340.                 resetline();
  341.                 if (chr == 'q')
  342.                     return(0);
  343.             }
  344.             ay = 0.0;
  345.             c = 0;
  346.             amorthead(printer);
  347.         }
  348.         a = p * l;
  349.         b = m - a;
  350.         p = p - b;
  351.         ay = ay + a;
  352.         fprintf(printer,"%s      %03d %11.2f %11.2f %11.2f %11.2f\n"
  353.             ,mnths[c],z+1,p,m,b,a);
  354.         fflush(printer);
  355.         if (p > 0.0)
  356.             c++;
  357.         else
  358.             break;
  359.     }
  360.     fprintf(printer,"\n\nYear of loan: %d\n",yr+1);
  361.     fprintf(printer,"\t$%11.2f for %d months at %11.2f %%\n",k,L,r);
  362.     fprintf(printer,"\tTotal interest paid during year --> $%11.2f\n",ay);
  363.     at = at + ay;
  364.     fprintf(printer,"\tTotal interest paid during loan --> $%11.2f\n",at);
  365.     fflush(printer);
  366.     myret();
  367.     if(prtflg == 'Y' || prtflg == 'y')
  368.         fclose(printer);
  369. }
  370.  
  371. amorthead(printer)
  372. FILE *printer;
  373. {
  374.     if(printer != stderr)
  375.         fprintf(printer," "); /* A bleedin' form feed.... */
  376.     else
  377.         clear();
  378.     fprintf(printer,"      PAYMENT  REMAINING     MONTHLY    PRINCIPAL   INTEREST\n");
  379.     fprintf(printer,"MNTH  NUMBER   PRINCIPAL     PAYMENT     PAYMENT     PAYMENT\n");
  380.     fflush(printer);
  381. }
  382.  
  383. iniline()
  384. {
  385.     struct sgttyb ntsgb;
  386.  
  387.     if(isatty(fileno(stdin)))
  388.     {
  389.         ioctl(fileno(stdin), TIOCGETP, &otsgb);
  390.         ntsgb = otsgb;
  391.         ntsgb.sg_flags |= CBREAK;
  392.         ioctl(fileno(stdin), TIOCSETP, &ntsgb);
  393.     }
  394. }
  395.  
  396. resetline()
  397. {
  398.     if(isatty(fileno(stdin)))
  399.     {
  400.         ioctl(fileno(stdin), TIOCSETP, &otsgb);
  401.     }
  402. }
  403. myret()
  404. {
  405.     fprintf(stdout,"\n\tPress RETURN to continue");
  406.     fflush(stdout);
  407.     iniline();
  408.     getchar();
  409.     resetline();
  410. }
  411.  
  412. char PC;
  413. short ospeed;
  414. int numlines;
  415. char *clr;
  416. char clbuf[20];    /* contains 'cl' sequence */
  417.  
  418. clear_init()
  419. {
  420.     char *tgetstr();
  421.     char *getenv();
  422.     char *buffer;    /* termcap info */
  423.     char *pbp;    /* contains pad character */
  424.     char *malloc();
  425.     char *cbp = clbuf; /* contains 'cl' sequence */
  426.  
  427.     pbp = malloc(20);    /* we're screwed anyway so don't bother */
  428.     buffer = malloc(1024);    /* to look at what was returned? */
  429.         ospeed = otsgb.sg_ospeed; /* was set in iniline routine */
  430.         tgetent(buffer, getenv("TERM")); /* get entry from /etc/termcap */
  431.         clr = tgetstr("pc", &pbp);    /* get pad character */
  432.     PC = (*clr ? *clr : 0);        /* NULL or actual character */
  433.         clr = tgetstr("cl", &cbp);    /* get clear sequence */
  434.     numlines = tgetnum("li");    /* get number of lines on screen */
  435.     free(pbp);
  436.     free(buffer);
  437. }
  438.  
  439. #undef  putchar
  440. int putchar();
  441.  
  442. clear()
  443. {
  444.         if (clr)
  445.                 tputs(clr, numlines, putchar);
  446.     printf("\n");    /* What the heck */
  447. }
  448.