In: Computer Science
Write a program that will accept user input for an initial deposit and a total amount the user wants to have, and will output the number of years it will take to reach his/her goal. For the basic program, the user will deposit the initial amount in a new account, and then the account will receive interest, compounded MONTHLY, at a rate of 0.5%. Then modify it by allowing the user to set a fixed amount to be deposited into the account at the beginning of every month. Again, the amount will be input by the user, but will be the same amount every month.
import java.util.Scanner;
public class Goal {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
//reading initial deposit
System.out.println("Enter initial
deposit: ");
double initial =
sc.nextDouble();
//reading target amount
System.out.println("Enter total
amount you want");
double amount =
sc.nextDouble();
int months = 0;
int years=0;
System.out.println("Enter monthly
deposit amount: ");
double
monthlyAmount=sc.nextDouble();
//loop until user reaches his
target
while (initial < amount) {
//adding 5%
interest rate
initial =
initial + initial * 0.05;
initial+=monthlyAmount;
months++;
}
years=months/12;
months=months%12;
System.out.println(years+" Years
and "+months+" months ");
}
}