Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Import the Scanner class
- import java.util.Scanner;
- // Import the DecimalFormat class
- import java.text.DecimalFormat;
- public class Task_1 {
- public static void main(String[] args) {
- // Create a Scanner object
- Scanner myNum = new Scanner(System.in);
- // Prompt user to enter monthly saving amount
- System.out.print("Enter the monthly saving amount (e.g., 100): ");
- double saving = myNum.nextDouble();
- // Prompt user to enter annual interest rate
- System.out.print("Enter the annual interest rate (e.g., 5): ");
- double interest = myNum.nextDouble();
- // Calculate the monthly interest rate
- interest /= 1200;
- // Prompt user to enter monthly saving amount
- System.out.print("Enter the number of months (e.g., 3): ");
- int months = myNum.nextInt();
- // Calculate amount in the savings account after the given month
- double totalSaving = 0;
- for (int month = 1; month <= months; month++) {
- totalSaving = (saving + totalSaving) * (1 + interest);
- }
- // Round the number to 3 decimal places
- DecimalFormat numFormat = new DecimalFormat("#.00");
- // Display result
- if (months > 1) {
- System.out.println("The amount in savings account after " + months + " months: " + "$" + numFormat.format(totalSaving));
- } else {
- System.out.println("The amount in savings account after " + months + " month: " + "$" + numFormat.format(totalSaving));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement