As the title stated, and a bit more in depth, it's due soon but I can be a bit late. Further, I am using the try/catch method because the professor wants exceptions caught. He suggested earlier in the class I ask here if I need help so here I am. The actual formula for calculation is supposed to be future value = investment * (1 + interest rate) ^ years held. Some calculations work, others don't and I can't find a single reason why. Any help is appreciated, Thanks.

I tried at least 5 different methods of math.pow ((long)math.pow, double x and double y). I eventually settled on math.pow (invest*rate, years), with rate being the textbox + 1.

try
            {
                //setting up some numbers to be parsed. 
                double InvestO = double.Parse(Investment_Textbox.Text);
                double InterO = double.Parse(Interest_Textbox.Text);
                double YearO = double.Parse(Years_Textbox.Text);
                double RateO = 1+InterO;

                //the actual calculation
                Future_Textbox.Text = Math.Pow((InvestO * RateO), YearO).ToString("C");
            } // end of the try coding

I expected it to be accurate or at least match the test data the professor gave (2000 amount, 0.15 interest, 5 year=4022.71) but in actuality it gave me something like 43 trillion or more.

2 Answers

0
Caius Jard On

It's the interest that needs to be powered, not the whole amount. You've decided to use the formula:

(2000*1.15)^5

This expands to:

2300*2300*2300*2300*2300

Haven't done the math but quadrillions seems about right

If you're compounding 15 pc interest over 5 years it's:

2300 * 1.15 * 1.15 * 1.15 * 1.15 * 1.15

So initial amount boosted by 15 per event every year for 5 years

This means your prof's formula vs your formula is:

initial * (1+rate)^5      //prof's formula
(initial * 1+rate)^5      //your formula, brackets around 1+rate omitted for clarity of "what went wrong"

I'd hence suggest:

double rate = 0.15:
double initial = 2000;
int years = 5;
double total = initial * Math.Pow(1+rate, years);

All you have to do now is get the values from your textboxes into the variables:

double rate = double.Parse(rateTextbox.Text)/100; //put 15 in the text box, not 0.15

I'll leave assembling the whole thing to you (this is an academic exercise after all)


Some other tips for c# and coding in general:

  • when you're learning, or even when you're pro and doing something really complex, write your algorithm out in comments first then fill code underneath, keep the comments. As a learner you think in English, not c#. Not having to hold the algorithm in your head AND translate it at the same time, will help. Think English, write English, translate to c#
  • variable names declared inside a method block (and private variables of a class) start with lowercase letters. Public variables/properties start with uppercase letters. Methods always start with uppercase letters
2
Community On

I'm guessing your attempting this Accrued Interest Amount calculation (Principal + Interest) A = P(1 + r)t? If so your order of operations are not correct. You are taking 2001.15 ^ 5 in your example as the Math.Pow operation runs after all the inner calculations have been completed. The last line should read like below.

Future_Textbox.Text = (InvestO * Math.Pow((RateO), YearO)).ToString("C");