The problem
If you know the balance and the annual percentage interest rate, you can compute the interest on the next monthly payment using the following formula: interest = balance * (annualInterestRate / 1200)
Write a program that reads the balance and the annual percentage interest rate and displays the interest for the next month. The program output is illustrated in java below:
Enter balance and interest rate (e.g., 3 for 3%): 1000 3.5
The interest is 2.91667
Breaking it down
public static void main(String[] Strings) {
Scanner input = new Scanner(System.in);
System.out.print("Enter balance and interest rate (e.g., 3 for 3%): ");
double balance = input.nextDouble();
double annualInterestRate = input.nextDouble() / 100;
input.close();
double interest = calculateInterest(balance, annualInterestRate);
System.out.print("The interest is " + interest);
}
private static double calculateInterest(double balance,
double annualInterestRate) {
double interest = balance * (annualInterestRate / 12);
return interest;
}
Output
Enter balance and interest rate (e.g., 3 for 3%): 2500 4
The interest is 8.333333333333334