Click here to Skip to main content
12,999,010 members (35,060 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 18 Sep 2012

Internal Rate of Return (IRR) Calculation

, 18 Sep 2012
Rate this:
Please Sign up or sign in to vote.
Program to calculate the IRR value using C/C++ similar to the one available in Excel


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.


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

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))
  if(old == 0)
   old = npv;
   old = new;
  new = npv;
  if(i > 0)
   if(old < new)
    if(old < 0 && new < 0)
     highGuessRate = newguessRate;
     lowGuessRate = newguessRate;
    if(old > 0 && new > 0)
     lowGuessRate = newguessRate;
     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);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Questionpow Pin
Girish CSC14-Apr-17 20:02
memberGirish CSC14-Apr-17 20:02 
Questione.gNice work but still need some impovization Pin
rahul.peerbits19-Jul-13 19:52
memberrahul.peerbits19-Jul-13 19:52 
QuestionThanks for the article Pin
Bilaltm17-Oct-12 6:31
memberBilaltm17-Oct-12 6:31 
QuestionMax iterations Pin
YvesDaoust24-Sep-12 8:58
memberYvesDaoust24-Sep-12 8:58 
GeneralMy vote of 3 Pin
YvesDaoust24-Sep-12 8:53
memberYvesDaoust24-Sep-12 8:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 18 Sep 2012
Article Copyright 2012 by vivekshankars
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid