print statements and doubles

EDIT: looks like the OP got his homework fixed, then did a runner.


Every year (i.e. once per loop) you need to multiply the present balance by 1+i. That's it!

Other things:
- if you loop while n > 0 ... and you never change n ... then you loop for ever.
- you don't need that many variables; you could, in fact, just keep the present balance in p.
Last edited on
The n does not change in your loop.
You always break out from the loop on first iteration.

Therefore, your lines 20-26 do this:
1
2
3
4
if ( n > 0 ) {
  presentBalance = p*(1+i);
  presentBalance = (presentBalance*i)*(n-1) + presentBalance;
}

Is that what your pseudocode describes?
You should multiply the presentBalance each year by 1+i, not multiply n by 1+i. Think how your bank savings account works.
Last edited on
Have one variable to hold the present balance. You could actually use p if you wanted; otherwise initialise your present balance variable to p before the loop.

On each pass through the loop ... just multiply your present balance variable by 1+i. THAT ... IS ... IT.

Make sure that your loop runs exactly n times ... not runs indefinitely or breaks out on the first pass because you put a break statement in.
Topic archived. No new replies allowed.