Answer:
The solution is written in Java.
Version 1
- public class Main {
-
- public static void main(String[] args) {
-
- double monthlyDeposit; // monthly saving
- double monthlyIntRate = 0.05 / 12; // monthly interest rate
- double monthlyValue = 0; // initial value
-
- DecimalFormat df3 = new DecimalFormat("#.###"); // formatter
- Scanner input = new Scanner(System.in); // input object
-
- // Prompt user to input monthly saving
- System.out.print("Enter your monthly saving: ");
- monthlyDeposit = input.nextDouble();
-
- // apply formula to calculate monthly value for 6 months
- for(int i=1; i <=6; i++)
- {
- monthlyValue = (monthlyDeposit + monthlyValue) * (1 + monthlyIntRate);
- System.out.println("Month " + i + ": $" + df3.format(monthlyValue));
- }
- }
- }
Version 2
1. // Import libraries
5. public class Main {
6.
7. public static void main(String[] args) {
8.
9. double monthlyDeposit = 200.50; // monthly saving
10. double monthlyIntRate = 0.0375 / 12; // monthly interest rate
11. double monthlyValue = 0; // initial value
12.
13. DecimalFormat df3 = new DecimalFormat("#.###"); // formatter
14.
15. // apply formula to calculate monthly value for 6 months
16. for(int i=1; i <=6; i++)
17. {
18. monthlyValue = (monthlyDeposit + monthlyValue) * (1 + monthlyIntRate);
19. System.out.println("Month " + i + ": $" + df3.format(monthlyValue));
20. }
21. }
22. }
Step-by-step explanation:
Version 1
Firstly, let's define all the necessary variables to hold the value of monthly deposit, monthly interest rate and monthly value.
Next create a Scanner object and use it to get user input for monthly saving and assign it to variable monthlyDeposit.
Next create a for-loop that runs 6 iterations to apply the formula and calculate the monthly value from first month till 6th month and print it.
Version 2
Modified the code from version 1 by changing monthly interest rate to 0.0375/12 and assigns monthlyDeposit with the given value, 200.50.
Since the monthlyDeposit is given, the previous code segment to use Scanner object to get user input are removed.