Calculating Mortgage Payment for Loan Using Java Code
The below code is used to calculate the monthly payment for a Mortgage loan using Java programing language. There are some online Mortgage Loan Calculator which uses javascript, java or flash action script. In this java program we get loan amount, interest rate, loan term as input. You can compare the answer with this online Mortgage Loan Comparison Calculator
import java.math.*;
import java.text.*;
class MortgageCalculator {
public static void main(String arguments[]) {
//Program Variables using array
double []terms = {84,180,360};//Mortgage Loan Term
double []Rates = {0.0535, 0.055, 0.0575};//Mortgage Loan's Interest Rate
double loan = 200000;//Loan Amount
double term, interestRate; //Temporary variables to make things generic and clearer
DecimalFormat df = new DecimalFormat("$###,###.00"); //Formatting the output
//for each term and Rate starting from the first location of each array terms[] and Rates[]
for (int i=0;i<terms.length;i++){
//Calculates the Payment per month to be paid under mortgage scheme One
interestRate=Rates[i]; //take value from the array Rates[] to a temporary variable
term=terms[i]; //take value from the array terms[] to a temporary variable
double monthlyRate = (interestRate/12); //derive the monthly interest rate
//calculate the discount factor
double discountFactor = (Math.pow((1 + monthlyRate), term) -1) / (monthlyRate * Math.pow((1 + monthlyRate), term));
double payment1 = loan / discountFactor; //calculate payable amount per month
//Output for mortage scheme One
System.out.println("The Loan amount is:"+df.format(loan)+"\n");
System.out.println("The intrest rate is:"+interestRate*100+"%");
System.out.println("The term of the loan is:"+term/12+" years.");
System.out.println("Monthly Payment Amount: " + df.format(payment1)+"\n");
}
}
}
