Welcome to the forum!
When posting code, please use code tags. You can edit your post, highlight the code and click the <> button to the right of the edit window. This will cause the code to show up with line numbers, syntax highlighting and in a fixed-width font, all of which make it easier to read and comment on.
Now to your code.
Your
for
loop should go from the min rate to the max rate. Let's make it the actual rate, so it should be a double instead of an int:
for (double rate = minrate; rate <= maxrate; rate += 0.01) {
Your code to compute the balance (threepercentrate, fourpercentrate(), etc) isn't right for a couple of reasons: first, what if they what 5%? Or 3.3% or 18.125? You would need a single function that takes the rate as one parameter and computes the new balance. Second, the interest compounds. After the first year, you add the interest to the balance and the second year, you take the interest on the NEW balance.
Computing the new balance is pretty easy. You don't really need a function at all, so get rid of twopercentrate, threepercentrate and fourpercentrate. If balance is the starting balance for the year and rate is the interest rate then:
1 2
|
double interest = balance * rate; // compute the interest payment
balance += interest; // add that interest to the balance, creating the new balance.
|
Or with some simple math:
balance *= (1+rate);
Now the loop should print the current balance and then compute the new balance for next year.