This page provides a simple loan calculator written in C, along with a full explanation of the underlying formula and code logic. You enter the loan amount, annual interest rate (APR), and loan term in years, and the program outputs the monthly payment and total interest paid. The C code below implements the standard amortization formula, and the interactive calculator on this page uses the same math so you can verify your own C program’s output.
Formula
The monthly payment for a fixed-rate loan is calculated using the standard amortization formula:
M = P × (r × (1 + r)^n) / ((1 + r)^n − 1)
Where:
- M = monthly payment (in dollars)
- P = principal loan amount (in dollars)
- r = monthly interest rate = annual rate (APR) divided by 12, expressed as a decimal (e.g., 8% APR → r = 0.08/12 = 0.0066667)
- n = total number of monthly payments = loan term in years × 12
The total interest paid is simply the monthly payment multiplied by the number of payments, minus the principal: Total Interest = M × n − P.
Worked Example
Using the default values from the calculator: loan amount P = $20,000, APR = 8%, term = 5 years.
- Convert APR to monthly rate: r = 8 / 1200 = 0.0066667
- Total number of payments: n = 5 × 12 = 60
- Compute (1 + r)^n = (1.0066667)^60 ≈ 1.4898457
- Numerator: P × r × (1 + r)^n = 20000 × 0.0066667 × 1.4898457 ≈ 198.6461
- Denominator: (1 + r)^n − 1 = 1.4898457 − 1 = 0.4898457
- Monthly payment M = 198.6461 / 0.4898457 ≈ 405.53
- Total interest = 405.53 × 60 − 20000 = 24331.80 − 20000 = $4,331.80
The calculator above gives the same results: monthly payment $405.53 and total interest $4,331.80.
Assumptions
- Fixed interest rate for the entire loan term (no rate changes).
- Payments are made at the end of each month (ordinary annuity).
- Interest is compounded monthly at APR/12.
- No fees, taxes, insurance, or other charges are included.
C Program Implementation
Here is a complete, simple C program that implements the loan calculator. It reads the three inputs from the user, computes the monthly payment and total interest, and prints them with two decimal places.
#include <stdio.h>
#include <math.h>
int main() {
double principal, apr, years, monthly_rate, n, 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;
n = years * 12.0;
// Standard amortization formula
payment = principal * monthly_rate / (1 - pow(1 + monthly_rate, -n));
total_interest = payment * n - principal;
printf("Monthly payment: $%.2f\n", payment);
printf("Total interest: $%.2f\n", total_interest);
return 0;
}
To compile and run this program, save it as loan_calc.c and use a C compiler like GCC: gcc loan_calc.c -lm -o loan_calc (the -lm links the math library). Then run ./loan_calc.
How to Use the Program and Interpret Results
Run the program and enter the loan amount, APR, and term in years. The program will output the monthly payment and total interest. The monthly payment is what you would pay each month under a fixed-rate, fully amortizing loan. The total interest is the extra amount you pay over the life of the loan beyond the principal.
For example, a $20,000 loan at 8% APR for 5 years yields a monthly payment of $405.53 and total interest of $4,331.80. If you increase the term to 10 years, the monthly payment drops to about $242.66, but total interest rises to $9,119.20—a trade-off between monthly cash flow and long-term cost.
Common Mistakes and How to Avoid Them
- Using the annual rate directly in the formula: Always divide the APR by 12 to get the monthly rate. Forgetting this will produce a payment that is far too high.
- Mixing up the number of payments: The exponent n must be the total number of monthly payments, not the number of years. Multiply years by 12.
- Ignoring rounding: The C program uses
doublefor precision, but when you print with%.2f, it rounds to cents. In real life, lenders round the payment to the nearest cent, and the total interest is then computed from that rounded payment. The calculator on this page does the same. - Not linking the math library: The
powfunction requires-lmwhen compiling with GCC. Without it, you’ll get a linker error. - Entering the interest rate as a decimal: If you enter 0.08 instead of 8, the program will treat it as 0.08% APR, giving a much lower payment. Always enter the rate as a percentage (e.g., 8 for 8%).
Limitations of the Simple Formula
The formula assumes a fixed interest rate and level payments. Real loans may have variable rates, interest-only periods, balloon payments, or additional fees. This simple calculator does not account for:
- Loan origination fees or closing costs
- Private mortgage insurance (PMI) if your down payment is less than 20%
- Property taxes and homeowners insurance (often escrowed)
- Prepayment penalties or the ability to make extra payments
- Daily interest accrual for loans with unusual payment dates
For such cases, you need a more detailed amortization schedule that includes these factors.
Frequently Asked Questions
How do I modify the C program to accept months instead of years?
Change the input prompt to ask for months, and set n directly to that value. For example, replace years with months and set n = months;. The rest of the formula remains the same.
What if the interest rate is 0%?
If the APR is 0%, the formula would divide by zero. In practice, a 0% loan is simply the principal divided by the number of payments. You can add an if statement in C to handle this: if apr == 0, payment = principal / n. The calculator on this page requires a minimum rate of 0.01% to avoid that issue.
How do I compute the total amount paid (principal + interest)?
Total paid = monthly payment × number of payments (M × n). In the C program, you can add printf("Total paid: $%.2f\n", payment * n);.
Can I use this program for a car loan or a personal loan?
Yes, as long as the loan has a fixed interest rate and equal monthly payments. The formula works for any amortizing loan, including auto loans, personal loans, and student loans. Just enter the appropriate amount, rate, and term.
Why does my payment differ from a bank’s quote?
Banks often include fees, insurance, or use slightly different rounding or compounding methods. They may also require a down payment that reduces the principal. Always check the lender’s disclosure for the exact payment amount. This calculator provides an estimate based on the basic formula.
This calculator and the C code give an estimate based on the standard amortization formula; your lender’s disclosure will show the exact payment and total cost.
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
- Auto Loan Old Car Value: How It Works$405.53
- Loan to Value Auto: How to Calculate LTV$405.53
- Auto Loan Calculator with Trade-In Value$405.53
- Personal Loan Rate Calculator Payment$405.53
- Simple Loan Calculator in C: Code & Example$405.53
- Loan to Cost Ratio Formula & How to Use It80.0 %