Net present value addresses the worth of a series of unequal and different sign cash flows. The NPV formula is: CF[1]/(1+i)^1+CF[2]/(1+i)^2+..CF[N]/(1+i)^N where CF are the cash flows in the respective periods, N is the number of periods and i is the considered interest rate. The NPV function would look like this: -------------------------------------- double pv(a,intrate,numflows) double a[],intrate; int numflows; { int i; double temp=0; for (i=1;i<=numflows;i++) { if (intrate==0) temp +=a[i-1]; else temp +=(a[i-1]/pow(1+intrate/100,i)); } return(temp); } If the interest rate is 0 then the denominator is 1. Therefore, if i=0 the net present value is the sum of cash flows. As i becomes larger NPV gets smaller. Consider the following example: Here's an example: You buy a mountain cabin for $25,000. During the first four years you are able to rent it out and you collect $3,500, $100, $4000 and $100 respectively. The fifth year you have a negative cash flow of $300 due to repairs. The cash flows for years 6 through 11 are : 4000 2300 -150 5600 321 -100. In the twelfth year you sell the cabin for $24,900. You ask yourself, would you have been better off depositing your $25,000 at 6% or is the series of cash flows generated by your investment greater in value? Solving the NPV equation for 6% gives a net present value of $26,675.49 or $1,675.49 more than your initial investment. Plotting the Net Present Value as a function of i between -5% and 100% will illustrate the inverse proportionality between the two values. | |. NPV(0)=$44,271 |-. Net Present Value | . as a function of interest |- . | . NPV(5)=$28,858.16 |- . NPV(6)=$26,675.49 | . |- . . . . NPV(20)=$11,091.59 | -=-=-=-=-=-=-=-=-=-=-=-=- | | | | | Internal Rate of Return ---------------------------- When dealing with multiple cash flows, often times the required value is the interest rate that would generate the NPV equal to the initial investment. In our example, the interest rate that would generate a $25,000 NPV to match the initial investment is 6.847%. This interest rate is called the Internal Rate of Return (IRR) and it cannot be computed analytically. There are instances when several solutions are possible and sometimes there are no solutions. The program should look only for solutions that are financially acceptable (i.e. between 0% and 200%) and it should return the lowest if more than one are available. The irr function should look for a solution by moving on the graph from 0% to the right in assigned increments while trying to match the initial investment with the NPV generated by the current interest rate. If the difference between Investment and NPV changes signs, then the function has moved too far and it should step back to the prior tick and continue with smaller increments. This process should invoke itself until the difference is smaller then a ten thousandth of the NPV. This time the program will accept input from a file. Note that the program expects the name of the input file as its argument. However, if no arguments are passed the user is prompted for the file name. Note that the getvals function uses pointers to the variables in order to be able to modify their values globally. The irr function keeps track of the increment and the current interest rate in the same manner: irr(a,invest,&intrate,&inc,numflows) However note that when the function is invoking itself recursively the "&" address symbol is not used because you are already dealing with a pointer. The program reads the annual interest rate, the initial investment and the cash flows. If the interest rate is not 0 the NPV is calculated. Else, the programs assumes that the user is searching for IRR. To run the program with the example above create a file containing the values on separate lines or on the same line. E.g. file "data.dat" contains the line: ------------------------------------------------------------------- 0 -25000 3500 100 4000 100 -300 4000 2300 -150 5600 321 -100 24900 ------------------------------------------------------------------- IRR and NPV program listing: ------------------ #include