calculate-loan

01 Loan payments

Simple Loan Calculator in C: Code & Example

Calculator No. 01-29612 · Updated · Formula and assumptions shown below

Monthly payment $405.53
Total interest
$4,331.67
= Monthly payment × loan term × 12 − loan amount
Loan amount
$20,000
Input
Interest rate (APR)
8 %
Input
You enter
Loan amount ($), Interest rate (APR) (%), Loan term (years)
You get
Monthly payment ($), Total interest ($)
Rounding
2 dp

Estimate for education, not financial advice. The result follows from the numbers you enter and the formula shown. It is not a quote or an offer, and it does not include fees, taxes or insurance unless the calculator asks for them. Ask your lender for the exact figures.

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.

  1. Monthly rate r = 8 / 1200 = 0.0066666667
  2. Number of payments n = 5 × 12 = 60
  3. Payment = 20000 × 0.0066667 / (1 − (1 + 0.0066667)^−60)
  4. Compute (1 + r)^−60 = (1.0066667)^−60 ≈ 0.671216
  5. Denominator = 1 − 0.671216 = 0.328784
  6. Numerator = 20000 × 0.0066667 = 133.3333
  7. Payment = 133.3333 / 0.328784 ≈ 405.53
  8. 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 / 100 and then use it for monthly payments, which grossly overestimates the payment.
  • Integer division in C: If you declare apr as an int, apr / 1200 becomes integer division, yielding 0 for most rates. Use double for all financial variables.
  • Forgetting to link the math library: Without -lm, the linker may not find pow(), 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;

Related

Formula used
in p | Loan amount | $ | 20000 | 1 | 100000000
in apr | Interest rate (APR) | % | 8 | 0.01 | 100
in yrs | Loan term | years | 5 | 0.0833 | 50
out pmt | Monthly payment | $ | p * (apr / 1200) / (1 - (1 + apr / 1200) ^ -(yrs * 12)) | 2
out int | Total interest | $ | pmt * yrs * 12 - p | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard loan formula for this calculation, fixed rate and monthly compounding unless stated, rounded as each result states. Not financial advice; no lender, rate or product is recommended. See formulas and assumptions, the disclaimer or report an error.

More in this section

The whole Loan payments section →