in the above formula i have values called npv and c0,c1,c2,c3,c4 and then n values .could u please tell me how to calulate r value in c#
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Nov 1, 2013, 7:28 AM EDT
It's not possible to solve the formula directly for 'r' so you have to use an iterative method which, starting from a guess, (hopefully) converges to an answer after a reasonable number of iterations.
Although there isn't a method to do this in the .NET framework's Math class, there is a method in VB.NET's Financial Module called IRR which is documented here:
http://msdn.microsoft.com/en-US/library/w8h9s5b4(v=vs.90).aspx
This method can be used from C# by first adding a reference to Microsoft.VisualBasic.dll to your project and then using code such as the following.
For convenience, I've used the results of my first post so we can work backwards to obtain r = 0.1. Notice that npv needs to included (negatively) as the first element of the array and also that the first cash flow (at time 0) needs to be added to this as it's paid in advance rather than at the end of the first year as IRR assumes:
As expected the output is:
Interest rate is 0.1 per unit p.a.
VulpesRank #55 · 29.2K reputationPosted Nov 1, 2013, 6:28 AM EDT
The output is:
Net present value is 8.30277986476333
Sasi ReddyRank #4,458 · 342 reputationPosted Nov 1, 2013, 10:29 PM EDT
That was working fine for me and thank you so much .The link was very useful for me
http://msdn.microsoft.com/en-US/library/w8h9s5b4(v=vs.90).aspx
Sumit KumawatRank #4,008 · 372 reputationPosted Aug 14, 2014, 9:45 PM EDT
Thanx for sharing this link...
But i want this in C# so it will be good for me if u can give me code for calculating NPV in c#.
Thanx in advance :)
VulpesRank #55 · 29.2K reputationPosted Aug 15, 2014, 1:35 AM EDT
Admittedly, we're using the function IRR from the VB runtime library but there's nothing wrong with that - it's all .NET :)
Sumit KumawatRank #4,008 · 372 reputationPosted Aug 18, 2014, 9:25 PM EDT
i gone through below link
http://msdn.microsoft.com/en-US/library/w8h9s5b4(v=vs.90).aspx
but when i calculate the NPV from some online NPV calculator with same values (given in the above link) the results are different.
below are link where i calculate the NPV
http://easycalculation.com/finance/net-present-value.php
http://www.datadynamica.com/irr.asp
VulpesRank #55 · 29.2K reputationPosted Aug 19, 2014, 6:00 AM EDT
So, if we take the first online calculator, if you put in the following:
Discount Rate 10%
Initial Invest Amount 0
Number of Years 3
Cash flows:
Year 1 = 2
Year 2 = 3
Year 3 = 4
then the net present value is 7.31.
However, you then need to add to this the first cash flow of 1 paid annually in advance (which therefore has a present value of 1) giving a total of 8.31 which is close to the 'exact' answer of 8.30277986476333 we obtained above.