user5928703 - 6 months ago 37

Java Question

Background: doing a budget portfolio program and I am trying to add in a compound interest calculator for a client's savings. Running into some problems here.

So here is the formula I currently have.

`double comprinc= 25*(Math.pow((1+.05/12),(12*year)));`

double futurev = saving1*(Math.pow((1+.05/12),((12*year)-1))/(.05/12));

This is the following formula broken down into two halves

Total = [ P(1+r/n)^nt ] + [ PMT * (((1 + r/n)^nt - 1) / (r/n)) ]

or

Total = comprinc + futurev

P= principle =25

r= rate=.05

n= number of time interest is compounded

t= the number of years =5( have year currently set to 5)

PMT= initial savings=saving1=25

The problem is that I am testing this against an official compound interest calculator

and the answers I'm getting are no where close.

For example the answer that my program reads out is $7700.28 after 5 years should be $1739.32

Answer

I think the key is the -1 is not part of the exponent. I am also trying to simplify here, by reducing calculations and parentheses.

```
double intPerPeriod = .05/12;
double numPeriods = 12*year;
double comprinc= 25*(Math.pow(1+intPerPeriod,numPeriods));
double futurev = saving1*(Math.pow(1+intPerPeriod,numPeriods)-1)/intPerPeriod;
```