Reputation: 7
Here's the prompt:
There is a bank account that earns 5 percent interest per year. The initial account balance is $10000. Given a target balance (for example, $20000), return the number of years that it takes for the account balance to reach the target balance. The number of years should be an integer.
Here's my code:
public int getNumberOfYears(double targetBalance){
int years = 1;
for (double balance = 10000.0; balance <= targetBalance; years++) {
double multiplier = Math.pow(1.05, years);
balance *= multiplier;
}
return years;
}
When this runs, [multiplier] increases by the following pattern:
1.05^1 --> (1.05^1)^2 --> ((1.05^1)^2)^3 --> etc.
How do I fix my code so that [multiplier] increases by the following pattern:
1.05^1 --> 1.05^2 --> 1.05^3 --> etc?
Upvotes: 0
Views: 735
Reputation: 32
You can use a while loop instead of for.
public int getNumberOfYears(double targetBalance){
int years = 1;
double balance = 10000.0;
while (1) {
if ( balance * 1.05 * years >= targetBalance) {
break;
}
else {
years++;
continue;
}
}
return years;
}
But you should consider on what happens to the interest earned on a year, is it being added to the current balance or added separately. If it is being added to the starting balance of the account consider following code.
public int getNumberOfYears(double targetBalance){
int years = 0;
double balance = 10000.0;
while (balance < targetBalance ) {
balance *= 1.05;
years ++;
}
return years;
}
Upvotes: 0
Reputation: 77
The following code will give you the answer in years. You can test it in excel by adding some formulas, the result is correct:
public class practice {
public static void main(String[] args) {
double interestRate = 0.05;
double initialAmount = 10000.00;
double targetValue = 20000.00;
int numYears = 0;
while(targetValue > initialAmount) {
initialAmount += (initialAmount * interestRate);
numYears++;
}
System.out.println("Year to hit target: " + numYears);
System.out.println("Final Amount: " + initialAmount);
}
}
Upvotes: 0
Reputation: 312219
You don't need to increase the multiplier - every year carries the same interest, and you just need to keep multiplying by the same interest rate every year:
public int getNumberOfYears(double targetBalance) {
int years = 1;
double multiplier = 1.05;
for (double balance = 10000.0; balance < targetBalance; years++) {
balance *= multiplier;
}
return years;
}
EDIT:
As a side note, this can be solved without a loop, by extracting a log of both sides of the equation:
10000 * 1.05year = target
1.05year = target / 10000
log(1.05year) = log(target / 10000)
year * log(1.05) = log(target / 10000)
year = log(target / 10000) / log(1.05)
Or, in java:
public int getNumberOfYears(double targetBalance) {
double years = Math.log(targetBalance / 10000) / Math.log(1.05);
return (int) Math.ceil(year);
}
Upvotes: 0
Reputation: 54168
When you do balance * Math.pow(1.05, years)
that means you directly apply the increase for years
, you want to go year by year, so just use the 1.05
.
Also for that kind of problem, a while
loop is prefered
public static int getNumberOfYears(double targetBalance) {
int years = 1;
double balance = 10000;
while (balance <= targetBalance) {
balance *= 1.05;
years++;
}
return years;
}
Upvotes: 0