View Full Version : How can I calculate the balance with this formula in java program

02-09-2012, 09:43 PM
If you deposit p dollars, the money in your account after n years at an interest rate r is p (1 + r)^n

This is what I got.... Is this correct? and would I need to cast this beacause years are in int variable?

double balance;
double dollars;
double interestRate;
int years;

balance = dollars * Math.pow((1 + interestRate), years);

02-10-2012, 04:22 PM
years does need to be in double form- whether you convert or cast in this situation i think depends on your style since double is more precise than int and there should be no chance of data loss (I could be wrong though)

Edit: Math.pow(double a, double b) is expected