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:
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:
 The cash flows with which we need to compute the IRR
 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);
}
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;
}
int main()
{
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);
}