Monday, January 30, 2023

Emi calculator in java code

Here's a simple Java code to calculate EMI (Equated Monthly Installment) using the formula:


import java.math.BigDecimal;

import java.math.RoundingMode;


public class EMI_Calculator {

  public static void main(String[] args) {

    double p = 10000;   // Loan amount

    double r = 0.05;    // Interest rate

    int n = 12;         // Loan tenure in months


    double emi = (p * r * Math.pow(1 + r, n)) / (Math.pow(1 + r, n) - 1);


    BigDecimal bd = new BigDecimal(emi).setScale(2, RoundingMode.HALF_UP);

    System.out.println("EMI: $" + bd.doubleValue());

  }

}


This code calculates the EMI for a loan of $10,000 with 5% interest rate for a tenure of 12 months. The output will be rounded to 2 decimal places using BigDecimal class. 

No comments:

Post a Comment

How ChatGPT can Benefit Coding: Your Guide to Leveraging an AI Language Model

 Introduction: Hello, coders! Welcome to this blog post on how ChatGPT, an AI language model, can benefit your coding skills and projects. A...