Skip to content
C project #05Beginner

Loan EMI Calculator

Compute the monthly EMI for a loan and print a full amortisation schedule — monthly for short loans, yearly for long ones.

Estimated time
~2 hours to build
Files
5 files · 219 lines
Technologies
C11, <stdio.h>, <math.h>

Introduction

Every home, car and education loan in India is repaid in Equated Monthly Instalments. This project implements the EMI formula and the amortisation schedule that banks print, which shows how each payment splits between interest and principal.

Problem statement

Read the loan amount, annual interest rate and tenure (in months or years). Print the EMI, the total amount payable and the total interest, then a schedule showing, for each month (or each year for loans longer than two years), the principal repaid, the interest paid and the remaining balance.

Objectives

  • Translate a financial formula into C using pow()
  • Simulate a process month by month with a loop
  • Handle a zero-interest edge case
  • Aggregate monthly values into yearly summaries

Features

  • EMI, total payment and total interest
  • Monthly schedule for loans up to 24 months
  • Yearly summary for longer loans
  • Zero-interest loans and last-month rounding handled correctly

Concepts demonstrated

  • Floating-point arithmetic
  • Loops
  • Functions
  • Edge cases

System architecture

emi implements P·r·(1+r)ⁿ / ((1+r)ⁿ − 1) with r = annual rate / 12 / 100, special-casing r = 0. schedule walks month by month: interest = balance × r, principal = EMI − interest, balance −= principal. In the last month the remaining balance is paid exactly, absorbing rounding. For long loans it sums twelve months before printing a row.

Folder structure

TextProject tree
loan-emi-calculator/
├── README.md
├── Makefile
├── src/main.c
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.c
emi (formula), schedule (amortisation loop with monthly/yearly modes) and input validation in main.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
Loan amount (Rs):
Annual interest rate (%):
Tenure (number followed by m or y, e.g. 18 m):
Monthly EMI : Rs 17180.79
Total payment : Rs 103084.73
Total interest : Rs 3084.73 (3.1% of the loan)
Month EMI Principal Interest Balance
1 17180.79 16305.79 875.00 83694.21
2 17180.79 16448.46 732.32 67245.75
3 17180.79 16592.39 588.40 50653.36
4 17180.79 16737.57 443.22 33915.79
5 17180.79 16884.03 296.76 17031.76
6 17180.79 17031.76 149.03 0.00
Input
100000
10.5
6 m

Testing

  • Compare with tests/expected-output.txt: Rs 1,00,000 at 10.5% for 6 months gives an EMI of Rs 17,180.79.
  • Try 20 y at 8.5% for a yearly summary.
  • Try 0% interest: the EMI must be the amount divided by the months.

Common errors

undefined reference to `pow'

Add -lm to the gcc command on Linux.

EMI shows as inf or nan

The rate was 0 and the zero-interest branch was removed; dividing by (1+r)ⁿ − 1 = 0 gives infinity.

Possible improvements

  • Show the effect of a prepayment on tenure and interest
  • Export the schedule to CSV
  • Compare two loan offers side by side

Additional challenges

  1. 1Given a desired EMI, compute the maximum loan amount.
  2. 2Compute the tenure needed to repay a loan with a fixed EMI.
  3. 3Add an annual step-up EMI option (EMI increases by x% each year).

Built and tested it yourself? Mark it to update your dashboard.