# Internal Rate of Return (IRR) Calculation

, 18 Sep 2012
 Rate this:
Program to calculate the IRR value using C/C++ similar to the one available in Excel

## Introduction

IRR (Internal Rate of Return) is the most widely used financial indicator while assessing return on an investment or a project. It is defined as the discount rate which makes the net present value of the cash flows from the investment equal to zero. In Excel, we have IRR formula to compute the discount rate based on the cashflows for an investment/project. This program is written in C/C++ and provided the IRR rate to an accuracy of upto 6 decimal points.

## Background

IRR Rate is mathematically derived by assuming a rate and computing the formula so that the value becomes zero. I am providing an example to understand the formula used:

 Year/Cash Flow 0/-4000 1/1200 2/1410 3/1875 4/1050

The IRR rate (r) for this cash flow is given by the formula:

$\mathrm{NPV} = -4000+\frac{1200}{(1+r)^1} + \frac{1410}{(1+r)^2} + \frac{1875}{(1+r)^3} + \frac{1050}{(1+r)^4} = 0.$

Here, we need to assume the rate r and find out an optimum rate for which the NPV (Net Present Value) is zero. The program uses the same logic to calculate the IRR.

## Using the Code

For IRR calculation, we need the following inputs:

1. The cash flows with which we need to compute the IRR
2. The period for which we are computing the IRR for the above cashflows

With these inputs, we compute the NPV value and check if it is zero or to the nearest precision possible. The following parameters are used for the computation:

• LOW_RATE: The initial rate with which we compute the NPV. This is # defined as 0.01 (1%)
• HIGH_RATE: The highest rate upto which we should consider for computing NPV. This is # defined as .5 (50%)
• MAX_ITERATION: There is always a possibility of not being able to arrive at the rate for certain cashflows. This variable acts as a stopper for the number of iterations the code should check for NPV so as to ensure that the program doesn't go for an infinite loop.
• PRECISION_REQ: NPV value will not normally hit zero. We can find the NPV value with a precision upto a certain value. This is set as 0.00000001. Hence when the computed NPV is below this value, the calculation stops and the rate used will be the IRR.

Every time the new rate is arrived as an average rate of LOW and HIGH rate. Depending upon the NPV value, the LOW and HIGH rate are updated every time to drill down to IRR rate.

Following is the code snippet of the function computing IRR:

#define LOW_RATE 0.01
#define HIGH_RATE 0.5
#define MAX_ITERATION 1000
#define PRECISION_REQ 0.00000001
double computeIRR(double cf[], int numOfFlows)
{
int i = 0,j = 0;
double m = 0.0;
double old = 0.00;
double new = 0.00;
double oldguessRate = LOW_RATE;
double newguessRate = LOW_RATE;
double guessRate = LOW_RATE;
double lowGuessRate = LOW_RATE;
double highGuessRate = HIGH_RATE;
double npv = 0.0;
double denom = 0.0;
for(i=0; i<MAX_ITERATION; i++)
{
npv = 0.00;
for(j=0; j<numOfFlows; j++)
{
denom = pow((1 + guessRate),j);
npv = npv + (cf[j]/denom);
}
/* Stop checking once the required precision is achieved */
if((npv > 0) && (npv < PRECISION_REQ))
break;
if(old == 0)
old = npv;
else
old = new;
new = npv;
if(i > 0)
{
if(old < new)
{
if(old < 0 && new < 0)
highGuessRate = newguessRate;
else
lowGuessRate = newguessRate;
}
else
{
if(old > 0 && new > 0)
lowGuessRate = newguessRate;
else
highGuessRate = newguessRate;
}
}
oldguessRate = guessRate;
guessRate = (lowGuessRate + highGuessRate) / 2;
newguessRate = guessRate;
}
return guessRate;
}
//Call to the above function in main method
int main()
{
//Cash flows
double cf[30], irr = 0.00;
int numOfFlows;
cf[0] = -70000;
cf[1] = 12000;
cf[2] = 15000;
cf[3] = 18000;
cf[4] = 21000;
cf[5] = 26000;
numOfFlows = 6;
irr = computeIRR(cf, numOfFlows);
printf("\nFinal IRR: %.8f", irr);
}

## Share

United States
No Biography provided

 First Prev Next
 Thanks for the article Bilaltm 17-Oct-12 6:31
 Max iterations YvesDaoust 24-Sep-12 8:58
 My vote of 3 YvesDaoust 24-Sep-12 8:53
 Last Visit: 31-Dec-99 18:00     Last Update: 28-Aug-14 20:56 Refresh 1