In this post, I am going to write a Simple Java program to Calculate EMI
This program takes the principal amount, interest rate, and loan tenure (in months) as input and calculates the EMI (Equated Monthly Installment) using the compound interest formula. I am going to use the Scanner class to take input from the user and DecimalFormat class to format the output.
Here is the complete Java program to calculate EMI:
import java.text.DecimalFormat;
import java.util.Scanner;
/**
*
* @author agurc
*/
public class EMICalc {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//create Scanner obj to get input values
Scanner sc = new Scanner(System.in);
//create DecimalFormat obj to print output in decimal
DecimalFormat df = new DecimalFormat("0.00");
//define variables
double p,r,t,emi;
//Get the input values from user
System.out.print("Principal amount: ");
p = sc.nextDouble();
System.out.print("Interest rate: ");
r = sc.nextDouble();
System.out.print("Tenure (in months): ");
t = sc.nextDouble();
//calc interest rate
r = r / (12 * 100);
//Calculate EMI using compound interest forumula
emi = (p * r * Math.pow(1 + r, t)) / (Math.pow(1 + r, t) - 1);
//print output
System.out.println("EMI: " + df.format(emi));
}
}
Output:
Principal amount: 100000 Interest rate: 11.2 Tenure (in months): 16 EMI: 6757.34
Read the inline comments to understand each and every line of the above program.