Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java, computing the following formula

I am trying the write the formula in the image using Java. However, I get the wrong output. This is what I am so far for the computation.
Assume Input is: Loan Amount = 50000 and Length = 3 year.My output is currently "676.08" for ‘Monthly Payment’ and "25661.0" for "Total Interest Payment" ‘Monthly payment’ should be "1764.03" and ‘Total Interest Payment’ should be "13505.05" Code is: “//Program that displays the interest rate and monthly payment based on the loan amount and length of loan the user inputs.

import java.util.Scanner;

public class bankLoanSelection{

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public static void main(String[] args){
    
    Scanner input = new Scanner(System.in);

    //Declarations.

    double interestRate;
    final double n = 12;
    
    //Generates an account number in the range of 1 to 1000.

    int accountnumber = (int)(Math.random() * 1000)+1;

    //User input
    
    System.out.print("Enter Loan Amount: $");
    double L = input.nextDouble();
    System.out.print("Length of Loan (years): ");
    double i = input.nextDouble();

    //selection structure using if and if-else statements.

    if (L <= 1000)
    interestRate = .0825;
    else if (L <= 5000)
    interestRate = .0935;
    else if (L <= 10000)
    interestRate = .1045;
    else interestRate = .1625;
    
    
    //Computations. 

    double MonthlyPayment = L * (interestRate / n) * Math.pow(1 + interestRate / n, i * n) / Math.pow(1 + interestRate / n, i * n) - 1;
    double TotalAmountOfLoan = MonthlyPayment * i * n;
    double TotalInterest = L - TotalAmountOfLoan;

    //Output.

    System.out.println("Account Number: #" + accountnumber);
    System.out.println("Loan Amount: $" + L);
    System.out.println("Loan Length (years): " + i);
    System.out.println("Interest Rate: " + interestRate * 100 + "%");
    System.out.printf("Total Monthly Payment: $%.2f%n", MonthlyPayment); //Rounds second decimal
    System.out.println("Total Interest Payments: $" + TotalInterest);
    
}

}“ Formula

>Solution :

You can not put round parentheses between condition.

It should be:

double MonthlyPayment = L * ((interestRate / n) * Math.pow(1 + interestRate / n, i * n)) 
                           / (Math.pow(1 + interestRate / n, i * n) - 1);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading