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%.
import java.util.Scanner;
public class AccountDeposit {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
// user input for an initial
deposit
// user will deposit the initial
amount in a new account
System.out.print("Enter an initial
deposit :");
double principle =
sc.nextDouble();
// total amount the user wants to
have
System.out.print("Enter a total
amount the you wants to have :");
double totalAmount =
sc.nextDouble();
double time = 0;
// the account will receive
interest, compounded MONTHLY, at a rate of 0.5%.
double rate = 0.5;
int yearsCount = 0;
while(true)
/* Calculate compound interest */
{
double CI =
principle * (Math.pow((1 + rate / 100), time));
if(CI+principle>=totalAmount) break;
time++;
}
// the number of years it will take
to reach his/her goal
System.out.println("The number of
years it will take to reach your goal is "+(int)(time/12));
}
}