156k views
4 votes
Suppose you save $100 (double monthlyDeposit) each month into a savings account with an annual interest rate of 5%. Thus the monthly interest rate (double monthlyIntRate) is 0.05/12 = 0.00417 After the first month, the value of the account (double monthlyValue initialzed to 0) becomes (100 + 0)*(1+.000417) which equals 100.417 After the second month, the value in the account becomes (100 + 100.417) *(1+0.00417) which equals 201.252 or (value at the end of first month) *(1+ monthlyIntRate) After the third month the account now contains (100 + 201.252) *(1+0.00417) which equals 302.507 or (value at the end of second month) *(1+ monthlyIntRate) and so on Write a program that prompts the user to enter a monthly savings amount (could contain cents) and prints the value of the account at the end of each month up to and including the 6th month. DO NOT compute the value of the account for each month on paper and then plug it into the program. The program follows the following pattern: monthlyValue = (monthlyDeposit + monthlyValue) * (1 + monthlyIntRate); 2. Using the same code as above, change the monthly deposit to $200.50 and interest rate to 3.75% and run the above program Remember to print the header in the format described in Lab2. Comment your code meaningfully.

1 Answer

5 votes

Answer:

The solution is written in Java.

Version 1

  1. public class Main {
  2. public static void main(String[] args) {
  3. double monthlyDeposit; // monthly saving
  4. double monthlyIntRate = 0.05 / 12; // monthly interest rate
  5. double monthlyValue = 0; // initial value
  6. DecimalFormat df3 = new DecimalFormat("#.###"); // formatter
  7. Scanner input = new Scanner(System.in); // input object
  8. // Prompt user to input monthly saving
  9. System.out.print("Enter your monthly saving: ");
  10. monthlyDeposit = input.nextDouble();
  11. // apply formula to calculate monthly value for 6 months
  12. for(int i=1; i <=6; i++)
  13. {
  14. monthlyValue = (monthlyDeposit + monthlyValue) * (1 + monthlyIntRate);
  15. System.out.println("Month " + i + ": $" + df3.format(monthlyValue));
  16. }
  17. }
  18. }

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.

User Alextoul
by
5.0k points