1) Write a program that accepts the amount of money deposited in a bank account, the annual interest rate and the target amount (The amount of money the account holder wants to have in the account after a period of time) and then calculates the number of years it will take for the money to accumulate to the targeted amount. NB: 1) The interest being earned is Compound Interest. 2) Don’t use the formula for calculating compound interest.
For example if the money deposited is 10000 and the target amount is 20000 and the account earns an interest rate (compound) of 10% pa, then the output should be: -
It will take 8 years for your money to reach your target.
By the end of this period, the amount in your account will be 21435.89
import java.text.DecimalFormat;
import java.util.Scanner;
public class Test {
public int accumulation(double deposited, double compoundInterest, double targetAmount){
DecimalFormat decimalFormat = new DecimalFormat("#.##");
double currentTargetAmount = deposited;
int years = 0;
double multiplier = compoundInterest / 100;
do {
currentTargetAmount += currentTargetAmount * multiplier;
years++;
}
while (targetAmount > currentTargetAmount);
System.out.println("It will take " + years + " years for your money to reach your target.");
System.out.println("By the end of this period, the amount in your account will be " + decimalFormat.format(currentTargetAmount));
return years;
}
public static void main(String args[]){
Test test = new Test();
Scanner scanner = new Scanner(System.in);
double deposited, annualInterestRate, targetAmount;
System.out.print("Enter amount of money deposited in a bank account: ");
deposited = scanner.nextDouble();
System.out.print("Enter annual interest rate: ");
annualInterestRate = scanner.nextDouble();
System.out.print("Enter target amount: ");
targetAmount = scanner.nextDouble();
test.accumulation(deposited, annualInterestRate, targetAmount);
}
}
Comments
Leave a comment