Compute the monthly EMI for loans using EMI = P·r·(1+r)ⁿ / ((1+r)ⁿ − 1) with r the monthly rate, and print the total payment and total interest.
Solution
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextDouble()) {
double principal = in.nextDouble();
double annualRate = in.nextDouble();
int months = in.nextInt();
double r = annualRate / 12 / 100;
double factor = Math.pow(1 + r, months);
double emi = r == 0 ? principal / months : principal * r * factor / (factor - 1);
double total = emi * months;
System.out.printf("Rs %,.0f at %.2f%% for %d months: EMI %,.2f, total %,.2f, interest %,.2f%n",
principal, annualRate, months, emi, total, total - principal);
}
}
}
Output Verified by compiling and running
Rs 500,000 at 9.50% for 60 months: EMI 10,500.93, total 630,055.84, interest 130,055.84
Rs 1,200,000 at 8.40% for 240 months: EMI 10,338.05, total 2,481,132.95, interest 1,281,132.95
Rs 60,000 at 0.00% for 12 months: EMI 5,000.00, total 60,000.00, interest 0.00
The formula divides by (1 + r)ⁿ − 1, which is 0 when the rate is 0 – so the zero-interest case is handled separately with the conditional operator.
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!