Home > front end >  Java, computing the following formula
Java, computing the following formula

Time:02-05

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{

    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

CodePudding user response:

You need to correct the arrangement of brackets in the denominator, the correct formula would be:

double numerator = L * (interestRate / n) * Math.pow(1   interestRate / n, i * n)
double denominator = Math.pow(1   interestRate / n, i * n) - 1;
double MonthlyPayment = numerator / denominator;

Try to break the problem into smaller units to solve.

CodePudding user response:

You have to 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);
  •  Tags:  
  • Related