# Java Mortgage Calculator

Java Mortage Calculator | Mortgage calculators are automated tools that enable users to determine the financial implications of changes in one or more variables in a mortgage financing arrangement.

Fixed-Rate Mortgage Loan Calculators are used by consumers to determine monthly repayments, and by Mortgage providers to determine the financial suitability of a home loan applicant. Understanding your mortgage helps you make better financial decisions.

In this post, we will develop a simple and basic Java program for the Mortage calculator. For these fixed loans, use the formula below to calculate the payment. Mortgage Monthly Payment Formula,

Monthly Payment = P [{r*(1+r)^n}/{(1+r)^n – 1}]

In this Mortgage monthly payment formula,

• P:- The loan amount or principal, which is the home purchase price plus any other charges, minus the down payment.
• r:- The annual interest rate on the loan.
• n:- The number of years you have to repay, also known as the term.

Example:- Assume you borrow 1,75,000 at 5% for 9 years, to be repaid monthly. What is the monthly payment? The monthly payment is 2015.6, and the total interest payable is 42,676.46.

## Method

The major variables in a mortgage calculation include loan principal, balance, periodic compound interest rate, number of payments per year, total number of payments and the regular payment amount, and e.t.c. More complex calculators can take into account other costs associated with a mortgage, such as local and state taxes, and insurance.

Java Method for Mortgage Calculator

``````// method for Mortgage calculator
public static double calculator(double principle, double rate,
double time) {

// convert rate for month format
rate = (rate/100)/12;

// convert time in the terms of months
time = time * 12;

// M = P [{r*(1+r)^n}/{(1+r)^n – 1}]
double payment = principle * (  (rate * Math.pow(1+rate, time))
/ (Math.pow(1+rate, time) - 1) );
return payment;
}``````

The input value for the rate will be taken in the form of percentage rate per the entire year, but we require this on a monthly basis. Therefore we must covert rate for monthly basis using the formula:- rate = (rate/100)/12

Similarly, the time period is also given in the form of years, which also should be converted into the months using the formula:- time = time * 12

To calculate the power of a number we will use the pow() method of the java.lang.Math class. See:- How to import Java Math class.

## Java Program for Mortgage Calculator using Method

Now let us see a Java program to see the code for basic Mortgage calculator using method. The input values will be taken from the end-user and result will be displayed to the screen.

``````import java.util.Scanner;

public class Mortgage {

public static void main(String[] args) {
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);

// variables
double principle = 0.0;
double rate = 0.0;
double time = 0.0;
double payment = 0.0;

// take input values
System.out.print("Enter Principal Amount: ");
principle = scan.nextDouble();
System.out.print("Enter Interest Rate: ");
rate = scan.nextDouble();
System.out.print("Enter Time (in years): ");
time = scan.nextDouble();

// calculate monthly payment
payment = calculator(principle, rate, time);

// display result
System.out.println("Monthly Payment = " + payment);

// close scan
scan.close();

}

// method for Mortgage calculator
public static double calculator(double principle, double rate,
double time) {
// convert rate for month format
rate = (rate/100)/12;

// convert time in the terms of months
time = time * 12;

// M = P [{r*(1+r)^n}/{(1+r)^n – 1}]
double payment = principle * (  (rate * Math.pow(1+rate, time))
/ (Math.pow(1+rate, time) - 1) );
return payment;
}

}``````

Output for the different test-cases:-

Enter Principal Amount: 100000
Enter Interest Rate: 6
Enter Time (in years): 30
Monthly Payment = 599.5505251527569

Enter Principal Amount: 180000
Enter Interest Rate: 2.29
Enter Time (in years): 25
Monthly Payment = 788.6059389246642

Enter Principal Amount: 175000
Enter Interest Rate: 5
Enter Time (in years): 9
Monthly Payment = 2015.5228044638266

It was a very basic and simple program for the Mortage calculator which was only calculating the monthly payment amount. There are many other things which also should be considered.

Uses:- When purchasing a new home, most buyers choose to finance a portion of the purchase price via the use of a mortgage. Prior to the wide availability of mortgage calculators, those wishing to understand the financial implications of changes to the five main variables in a mortgage transaction were forced to use compound interest rate tables. These tables generally required a working understanding of compound interest mathematics for proper use. In contrast, mortgage calculators make answers to questions regarding the impact of changes in mortgage variables available to everyone.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!