Loan EMI Schedule
A lending app shows borrowers a month-by-month repayment table. Let round2(x) = Math.round(x * 100) / 100 and r = annualRatePercent / 12 / 100.
The monthly instalment is emi = round2(principal * r * (1 + r)^months / ((1 + r)^months - 1)), or round2(principal / months) when r is 0.
Start with balance = principal. For each month m from 1 to months: interest = round2(balance * r). In every month except the last, principalPart = round2(emi - interest) and payment = emi. In the last month the borrower clears whatever is left: principalPart = balance and payment = round2(principalPart + interest). Then balance = round2(balance - principalPart).
Return one row per month: { month, payment, interest, principal, balance } where principal is principalPart and balance is the balance after the payment.
Example 1
Input: principal = 10000, annualRatePercent = 12, months = 3 Output: [{"month":1,"payment":3400.22,"interest":100,"principal":3300.22,"balance":6699.78},{"month":2,"payment":3400.22,"interest":67,"principal":3333.22,"balance":3366.56},{"month":3,"payment":3400.23,"interest":33.67,"principal":3366.56,"balance":0}] Explanation: emi = 3400.22; the last row pays one extra paisa to clear the balance.
Example 2
Input: principal = 1200, annualRatePercent = 0, months = 4 Output: [{"month":1,"payment":300,"interest":0,"principal":300,"balance":900},{"month":2,"payment":300,"interest":0,"principal":300,"balance":600},{"month":3,"payment":300,"interest":0,"principal":300,"balance":300},{"month":4,"payment":300,"interest":0,"principal":300,"balance":0}]
Constraints
- 1 <= principal <= 10^8
- 0 <= annualRatePercent <= 36
- 1 <= months <= 360
Hint 1
Follow the rounding steps literally; rounding at each step is what real lenders do.
Hint 2
The final month absorbs the rounding drift so the balance ends at exactly 0.
Discussion 0
Join free to post in the discussion.
Good practice for: Backend Developer, Full Stack Developer, Blockchain Developer, Data Engineer, Automation Test Engineer, Mobile App Developer
Run checks your code on 3 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.