Mr. Mike wants to calculate a monthly mortgage
payment. Assume that the interest rate is compounded monthly.
Prompt the user for a double representing the annual interest
rate.
Prompt the user for the number of years the mortgage will be held.
( typical input here is 10,15 or 30)
Prompt the user for a number representing the mortgage amount
borrowed from the bank.
Write a Java Program that calculates the monthly mortgage
payment.
Output the summary of the mortgage problem as follows
The annual interest rate in percent notation.
The mortgage amount in Omani Rials.
The monthly payment in Omani Rials with only to significant digits
after the decimal point.
The total payment over the years with only two significant digits
after the decimal point.
The overpayment, i.e., the difference between the total payment
over the years and the mortgage amount.
Explanation:I have written the code for calculating the mortgage payment in the class MortgagePayment and have also shown the output, please find the image attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.
//code
import java.util.Scanner;
public class MortgagePayment {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
System.out.print("Enter the annual
interest rate:");
double interestRate =
input.nextDouble();
System.out.print("Enter number of
years the mortgage will be held:");
int noOfYears =
input.nextInt();
System.out.print("Enter the
mortgage amount borrowed from the bank:");
double mortgageAmount =
input.nextDouble();
double monthlyInterestRate =
(interestRate / 100) / 12;
int numberOfPayements = 12 *
noOfYears;
// formula for monthly mortgage
payment M = P [ i(1 + i)^n ] / [ (1 +
// i)^n – 1]
double monthlyPayment =
((mortgageAmount) * (monthlyInterestRate
* (Math.pow((1 + monthlyInterestRate),
numberOfPayements))))
/ ((Math.pow((1 + monthlyInterestRate),
numberOfPayements)
-
1));
System.out
.println("The annual interest rate is: " +
interestRate + "%");
System.out
.println("The mortgage amount is: " +
mortgageAmount + " OMR");
System.out.println("The monthly
payment is: "
+ Math.round(monthlyPayment * 100.0) / 100.0 + "
OMR");
double totalPayment =
monthlyPayment * numberOfPayements;
System.out.println("The total
payment is: "
+ Math.round(totalPayment * 100.0) / 100.0 + "
OMR");
double overPayment = totalPayment -
mortgageAmount;
System.out.println("The over
payment is: "
+ Math.round(overPayment * 100.0) / 100.0 + "
OMR");
input.close();
}
}
Output:
Get Answers For Free
Most questions answered within 1 hours.