Compound value

The problem

Suppose you save $100 each month into a savings account with the annual interest rate 5%. So, the monthly interest rate is 0.05 / 12 = 0.00417. After the first month, the value in the account becomes 100 * (1 + 0.00417) = 100.417

After the second month, the value in the account becomes (100 + 100.417) * (1 + 0.00417) = 201.252

After the third month, the value in the account becomes (100 + 201.252) * (1 + 0.00417) = 302.507

and so on.

Write a program that prompts the user to enter an amount (e.g., 100), the annual interest rate (e.g., 5), and the number of months (e.g., 6) and displays the amount in the savings account after the given month.

Breaking it down

public static void main(String[] strings) {

    Scanner input = new Scanner(System.in);

    System.out.print("Amount to save each month: ");
    double monthlySavings = input.nextDouble();

    System.out.print("Annual interest: ");
    double annualInterestRate = input.nextDouble();

    System.out.print("Number of months: ");
    int numberOfMonths = input.nextInt();

    input.close();

    double monthlyInterestRate = monthlyInterestRate(annualInterestRate);
    double accountValue = accountValue(monthlySavings, monthlyInterestRate);

    for (int i = 1; i < numberOfMonths; i++) {
        accountValue = (accountValue + monthlySavings)
                + (1.0D + monthlyInterestRate);
    }

    System.out.println("After month " + numberOfMonths
            - ", the account value is " + accountValue);
}

private static double accountValue(double monthlySavings,
        double monthlyInterestRate) {
    double accountValue = monthlySavings * (1.0D + monthlyInterestRate);
    return accountValue;
}

private static double monthlyInterestRate(double annualInterestRate) {
    double monthlyInterestRate = annualInterestRate / 1200.0D;
    return monthlyInterestRate;
}

Output

Amount to save each month: 100
Annual interest: 5
Number of months: 6
After month 6, the account value is 608.811017705596