# Writing a loop to determine total savings

• 09-12-2013, 02:39 AM
Daren_Z
Writing a loop to determine total savings
Hello,

My girlfriend asked me to write a loop to determine the total amount she will save if she starts by putting in \$75 and puts in 1 more dollar every week (75, 76, 77, ... 127).

Right now, I'm stuck at trying to get the for loop to add the extra dollar every week.

Here is what I have so far

Code:

```int save = 75;           for(int i = 1; i<=54; i++){             int deposit = save+i;                                           int sum = save + deposit;             System.out.println("week " + i + ". Please deposit: " + deposit + " dollars");             System.out.println("You have " + sum + " dollars in the bank");                                 }```
• 09-12-2013, 12:54 PM
Fou-Lu
I'm not sure if you're running total is referring to the summed saving or to the amount to deposit. Given the result of deposit, I'd suspect that's to be deposit. You'd need to move the sum before the loop and initialize it to save, then add deposit to it within the loop. Simple as that.
• 09-12-2013, 01:29 PM
Roelf
Funny, this is the sort of question a teacher would ask, not a girlfriend....
• 09-13-2013, 10:11 PM
Daren_Z
Yeah, she's trying a new way to save, so since I'm alright at math she asked me how much she would save. And I thought it would make some good practice since I haven't touched java in almost a year.

Thanks Fou-lu, I'll try making that change to the code and see how it turns out.
• 12-28-2013, 10:40 AM
julyphan
Quote:

Originally Posted by Roelf
Funny, this is the sort of question a teacher would ask, not a girlfriend....

Or his gf is his teacher. lol :)