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 static int accumulation(double amountOfMoney, double interest, double targetAmount){
DecimalFormat decimalFormat = new DecimalFormat("#.##");
double currentTargetAmount = amountOfMoney;
int years = 0;
double multiplier = interest / 100;
while (targetAmount > currentTargetAmount){
currentTargetAmount += currentTargetAmount * multiplier;
years++;
}
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[]){
Scanner scanner = new Scanner(System.in);
double AmountOfMoney;
double Interest;
double TargetAmount;
System.out.print("Enter amount of money: ");
AmountOfMoney = scanner.nextDouble();
System.out.print("Enter interest: ");
Interest = scanner.nextDouble();
System.out.println("Enter target amount: ");
TargetAmount = scanner.nextDouble();
System.out.print("It will take " + accumulation(AmountOfMoney, Interest, TargetAmount) + " years for your money to reach your target.");
}
}
Comments
Leave a comment