Advertisement
Ocha_DSH

Untitled

May 27th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. // Import the Scanner class
  2. import java.util.Scanner;
  3.  
  4. // Import the DecimalFormat class
  5. import java.text.DecimalFormat;
  6.  
  7. public class Task_1 {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         // Create a Scanner object
  12.         Scanner myNum = new Scanner(System.in);
  13.  
  14.         // Prompt user to enter monthly saving amount
  15.         System.out.print("Enter the monthly saving amount (e.g., 100): ");
  16.         double saving = myNum.nextDouble();
  17.  
  18.         // Prompt user to enter annual interest rate
  19.         System.out.print("Enter the annual interest rate (e.g., 5): ");
  20.         double interest = myNum.nextDouble();
  21.  
  22.         // Calculate the monthly interest rate
  23.         interest /= 1200;
  24.  
  25.         // Prompt user to enter monthly saving amount
  26.         System.out.print("Enter the number of months (e.g., 3): ");
  27.         int months = myNum.nextInt();
  28.  
  29.         // Calculate amount in the savings account after the given month by using a for loop
  30.         double totalSaving = 0;
  31.         for (int month = 1; month <= months; month++) {
  32.             totalSaving = (saving + totalSaving) * (1 + interest);
  33.  
  34.         }
  35.         // Round the number to 3 decimal places
  36.         DecimalFormat numFormat = new DecimalFormat("#.00");
  37.  
  38.         // Display result
  39.         if (months > 1) {
  40.             System.out.println("The amount in savings account after " + months + " months: " + "$" + numFormat.format(totalSaving));
  41.         } else {
  42.             System.out.println("The amount in savings account after " + months + " month: " + "$" + numFormat.format(totalSaving));
  43.         }
  44.  
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement