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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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:
1 2 3 4 |
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.