'How can I turn this compound value function into a for-loop?

Okay so I am new to Java. I have a program that calculates the compound value after asking the user for a savings amount and an annual interest rate. I'm having trouble getting it into a for loop but I feel like I'm somewhat close? The hard part in my mind is understanding how to get the last months total into the new calculation.

Here's my formula for the compound value currently:

    firstMonth = savingAmount * (1 + monthlyInterestRate); 
    secondMonth = (savingAmount + firstMonth) * (1 + monthlyInterestRate);
    thirdMonth = (savingAmount + secondMonth) * (1 + monthlyInterestRate);
    fourthMonth = (savingAmount + thirdMonth) * (1 + monthlyInterestRate);
    fifthMonth = (savingAmount + fourthMonth) * (1 + monthlyInterestRate);
    sixthMonth = (savingAmount + fifthMonth) * (1 + monthlyInterestRate);

It is ugly obviously and it should be in a for-loop. Again the savingAmount is user input and the annualInterestRate is user input. The the monthlyInterestRate is annualInterestRate/12.

Here is the for-loop I have so far.

    for (int i = 1; i <= 6; i++ );
    {
        sixthMonth = savingAmount * Math.pow(1+monthlyInterestRate, 6);
    }   

I am still learning for-loops but doesn't my code say it adds up while it is less than or equal to 6? And while those are true to do the formula I provided. No you don't have to answer the question but if you could lead me in the right direction that'd be great. So how would I start to convert it? Feel free to ask for more info if needed.



Solution 1:[1]

Try this:

for (int i = 1; i <= 6; i++) {
    monthAmount = (savingAmount + monthAmount) * (1 + monthlyInterestRate); 
}

This should get you to the same answer.

You were on the right track, but this will allow you to use the previous months amount in the formula. This code will also work for however long you want the loop to run for.

Solution 2:[2]

I would actually recommend using the compound interest formula A = P (1 + r/n) ^ nt.

  • A - final amount with compound growth
  • P - principal investment
  • r - annual interest rate
  • n - number of times the interest is compounded per time period
  • t - number of time periods compounded

So you could then reduce all of this to:

amount = savingAmount * Math.pow(1 + monthlyInterestRate/timesCompoundedPerMonth, 
    timesCompoundedPerMonth * monthsCompounded); 

Compound Interest Formula - Explained

Solution 3:[3]

I would recommending storing the amounts in a list for easy lookup if you want to see how much savings you will have at each month.

public static void main(String args[]) {

    double savingsAmount = 543.23;
    double annualInterestRate = 0.85; // %
    double monthlyInterestRate = annualInterestRate / 12;

    List<Double> savings = new ArrayList<Double>();

    savings.add(savingsAmount); // month 0

    int monthsInTheFuture = 6;
    double compoundInterest = 1 + monthlyInterestRate;
    for (int i = 1; i <= monthsInTheFuture; i++) {
        double previousSavings = savings.get(i-1);
        double nextSavings = previousSavings * compoundInterest;
        savings.add(nextSavings);
    }

    System.out.println(savings);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 The Guy with The Hat
Solution 2
Solution 3