Reputation: 89
I'm practicing while and for in java. The question is making a program that calculates the amount of years it will take a 2500 investment to be worth 5000 at an investment rate of 7.5%. Years keeps coming back as 1, which isn't the correct answer. I'm relatively new to Java and only know what I've learned in school so far. I know this can be done using a simple formula but the exercise requires using while, for, if, etc. Please help!
int years = 0;
int i = 2500;
while (i < 5000) {
double interest = i * 1.075;
i += interest;
years ++;
}
System.out.println("It will take " + years + " years for a $2,500 investment to be worth at least $5,000");
}
}
Upvotes: 0
Views: 480
Reputation: 723
Your multiplier is actually calculating 107.5% interest, which comes out to 2687.50 for the first year.
You should change your interest multiplier to .075 instead of 1.075:
double interest = i * .075;
Upvotes: 1