Skip to main content

Mortgage Calculator with User Input and Mathematical Expression - Example0005

Mortgage Calculator with User Input and Mathematical Expression - Example0005

In this code sample, we will see how mortgage(EMI) calculator can be made
Mortgage formula M=P * (r(1+r)**n)/((1+r)**n-1) will be used.
Scanner Class will be used to get user input.
We need to get the rightly formatted output (with dollar sign as currency). For this purpose we will use Class NumberFormat:NumberFormat.getCurrencyInstance.
We use NumberFormat.getCurrencyInstance method.

Keyword: java, mortgage calculator, user input, scanner, currency format, dollar sign, numberformat, getcurrencyinstance

package com.swprogramdeveloper;


import java.text.NumberFormat;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
// write your code here

        //Mortgage Calculator

        Scanner userInput = new Scanner(System.in);

        System.out.print("Please Input the Principal Amount : ");
        float principal = userInput.nextFloat();

        System.out.print("Please Input the Annual Interest Rate : ");
        float monthlyInterest = userInput.nextFloat() /12/100;

        System.out.print("Please Input Period of Mortgage in years : ");
        float mortgageMonths = userInput.nextFloat() * 12;

        System.out.println("Principal : " + principal + "Monthly Interest : " + monthlyInterest + " Mortgage Months : " + mortgageMonths);

        //Mortage Formula
        // M=P * (r(1+r)**n)/((1+r)**n-1)
        //principal * (monthlyInterest(1+monthlyInterest)**mortageMonths)/(((1+monthlyInterest)**mortageMonths-1)
        float multiplier= (float) Math.pow((1+monthlyInterest),mortgageMonths);
        float mortgage;
        mortgage = (principal*monthlyInterest*multiplier)/(multiplier-1);

        System.out.println("multiplier : " + multiplier);
        System.out.println("mortgage EMI : " + mortgage);
       
        // To get currency format, we use NumberFormat Class
        NumberFormat mortgageCurrency = NumberFormat.getCurrencyInstance();
        String finalMortgage=mortgageCurrency.format(mortgage);
        System.out.println("mortgage EMI(with currency style) : " + finalMortgage);

    }
}



Output0005
==========

Please Input the Principal Amount : 100000
Please Input the Annual Interest Rate : 3.92
Please Input Period of Mortgage in years : 30
Principal : 100000.0Monthly Interest : 0.0032666668 Mortgage Months : 360.0
multiplier : 3.2352057
mortgage EMI : 472.8128
mortgage EMI(with currency style) : $472.81

Process finished with exit code 0

Comments

Popular posts from this blog

Example to see how to get user input and do with mathematical expressions- Example0004

Example to see how to get user input and do with mathematical expressions- Example0004 In this example we are going to see how to get input from user, Mathematical expressions, For input we create an object from Class Scanner and with System.in Then we use method 'next' to get the input.  We will also see how the normal expression calculations are also done in Java // Input from keyboard, Mathematical expression package com.swprogramdeveloper; import java.util.Scanner; public class Main {     public static void main(String[] args) { // write your code here     // Constants         final float PI=3.14F;         float radius = 4.0F;         System.out.print("Please, enter radius of the circle : ");        //Scanner Class to get user input         Scanner radiusInput = new Scanner(System.in);        //NextFloat will read ...

Area of a Circle without new Class and Math Class-Example0009

Area of a Circle without new Class and Math Class-Example0009 This example is example for using Scanner class to get input from user and calculate area of a circle. Using of Class Math is also shown Also casting from double to float is also shown package com.swprogramdeveloper; Keyword: java, aoc, scanner, user input, casting, math class import java.util.Scanner; public class Main {     public static void main(String[] args) { // write your code here         final float PI=3.14F;         Scanner askinput= new Scanner(System.in);         System.out.print("Please Enter Radius of the Circle:");         float radius= askinput.nextFloat();         //Using Mathematical expression         float area= PI * radius * radius;         System.out.print("Area of Circle(PIXRXR):");         System.out.println(...