This page shows you how to build a simple loan calculator in the C programming language: you enter the loan amount, the annual interest rate, and the loan term in years, and the program outputs the monthly payment and total interest. Below you’ll find a complete, ready-to-run C program, a line-by-line explanation of the formula, and a worked example using realistic default values.
Formula
The calculator uses the standard fixed-rate loan payment formula:
M = P * (r * (1 + r)^n) / ((1 + r)^n – 1)
where:
- M = monthly payment (in dollars)
- P = principal, the loan amount (in dollars)
- r = monthly interest rate = (APR / 100) / 12, expressed as a decimal (e.g., 8% APR → 0.08/12 = 0.0066667)
- n = total number of monthly payments = loan term in years × 12
Total interest is then simply: Total Interest = M × n − P.
Complete C Program
#include <stdio.h>
#include <math.h>
int main() {
double principal, apr, years, monthly_rate, n, monthly_payment, total_interest;
printf("Enter loan amount ($): ");
scanf("%lf", &principal);
printf("Enter annual interest rate (%%): ");
scanf("%lf", &apr);
printf("Enter loan term (years): ");
scanf("%lf", &years);
monthly_rate = apr / 1200.0; // APR% to monthly decimal
n = years * 12.0;
monthly_payment = principal * monthly_rate / (1 - pow(1 + monthly_rate, -n));
total_interest = monthly_payment * n - principal;
printf("Monthly payment: $%.2f\n", monthly_payment);
printf("Total interest: $%.2f\n", total_interest);
return 0;
}
Compile with a C compiler (e.g., gcc loan.c -lm -o loan). The -lm links the math library for pow().
Worked Example
Let’s use the default values from the calculator block: loan amount $20,000, APR 8%, term 5 years.
- Monthly rate r = 8 / 1200 = 0.0066666667
- Number of payments n = 5 × 12 = 60
- Payment = 20000 × 0.0066667 / (1 − (1 + 0.0066667)^−60)
- Compute (1 + r)^−60 = (1.0066667)^−60 ≈ 0.671216
- Denominator = 1 − 0.671216 = 0.328784
- Numerator = 20000 × 0.0066667 = 133.3333
- Payment = 133.3333 / 0.328784 ≈ 405.53
- Total interest = 405.53 × 60 − 20000 = 24331.80 − 20000 = 4331.80
So the monthly payment is $405.53 and total interest is $4331.80 — matching the calculator output.
Assumptions
- Fixed interest rate for the entire loan term (no rate changes).
- Payments are made at the end of each month (ordinary annuity).
- Interest compounds monthly at APR/12.
- No fees, taxes, insurance, or other charges are included.
How to Read the Result
The monthly payment is the fixed amount you’d pay each month to fully amortize the loan by the end of the term. The total interest tells you the extra cost of borrowing over the entire period. For example, a $20,000 loan at 8% for 5 years costs $4,331.80 in interest, meaning you repay $24,331.80 in total.
Notice how the interest portion is highest in the first month and decreases over time; the principal portion increases. That’s the nature of amortization.
Common Mistakes and How to Avoid Them
- Using APR as a monthly rate directly: Always divide by 12 and by 100. Many beginners write
r = apr / 100and then use it for monthly payments, which grossly overestimates the payment. - Integer division in C: If you declare
apras anint,apr / 1200becomes integer division, yielding 0 for most rates. Usedoublefor all financial variables. - Forgetting to link the math library: Without
-lm, the linker may not findpow(), causing a compile error. - Rounding too early: Keep full precision in intermediate steps; only round the final output to cents. Rounding the monthly rate to 0.0067 instead of 0.0066667 can change the payment by a few cents.
Limitations of This Formula
This formula only works for fixed-rate, fully amortizing loans with equal monthly payments. It doesn’t apply to:
- Adjustable-rate loans (rates change over time)
- Interest-only or balloon payment loans
- Loans with fees or points (those require adjusting the principal or APR)
- Biweekly payment schedules (the formula changes)
For such loans, you’d need a more complex model. Also, the result is an estimate; the lender’s disclosure gives the exact figures.
Frequently Asked Questions
How do I calculate monthly payment if the term is in months instead of years?
Simply set n = number of months directly, and don’t multiply by 12. For example, a 60-month loan has n = 60. The rest of the formula stays the same.
What does APR mean and how is it different from the interest rate?
APR (Annual Percentage Rate) includes the interest rate plus certain fees and costs, expressed as a yearly rate. For a simple calculator, we treat APR as the annual interest rate. A lender’s APR may differ from the nominal rate if fees are included, but our formula uses the APR you input.
Can I use this calculator for a car loan or mortgage?
Yes, the math is the same for any fixed-rate amortizing loan. However, mortgages often have additional costs (property tax, insurance, PMI) that are not included here, so the actual monthly payment will be higher.
Why does my C program give a different result than this calculator?
Check for integer division, missing #include <math.h>, or using float instead of double. Also ensure you’re using the same formula and not rounding intermediate values.
What if the interest rate is 0%?
If APR is 0, the formula divides by zero. In practice, a 0% loan simply divides the principal by the number of payments. You can add an if statement in C to handle that case: if (apr == 0) payment = principal / n;