calculate-loan

01 Loan payments

Simple Interest Calculator in C: Formula and Code

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

Simple interest $100.00
Total amount
$1,100.00
= Principal + simple interest
Principal
$1,000
Input
Annual interest rate
5 %
Input
You enter
Principal ($), Annual interest rate (%), Time (years)
You get
Simple interest ($), Total amount ($)
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 simple interest calculator in C computes interest and the total amount from a principal, annual interest rate, and time in years. Enter those three values to see the interest and ending balance; the code uses the standard simple-interest formula and prints the results to two decimal places.

Formula

Simple interest is calculated as principal multiplied by the annual rate expressed as a decimal, then multiplied by time in years:

I = P × r × t

  • I is the interest earned or charged, in dollars.
  • P is the starting principal, in dollars.
  • r is the annual interest rate as a decimal. For an input rate in percent, divide it by 100.
  • t is time in years.

The ending amount is A = P + I. The calculator accepts the rate as a percentage, so its equivalent expression is I = P × APR ÷ 100 × t. The formula does not add previously earned interest to the principal during the period.

Worked Example

Using the calculator’s default inputs—$1,000 principal, 5% annual interest, and 2 years:

  1. Convert the rate to decimal form: 5 ÷ 100 = 0.05.
  2. Calculate interest: $1,000 × 0.05 × 2 = $100.
  3. Calculate the total: $1,000 + $100 = $1,100.

The results are $100.00 in simple interest and a $1,100.00 total amount, rounded to two decimal places.

Simple Interest Code in C

This standalone C program reads the principal, annual rate, and time in years from standard input. It applies the same formula and displays both results to two digits after the decimal point.

#include <stdio.h>

int main(void) {
    double principal;
    double annualRate;
    double timeYears;
    double interest;
    double totalAmount;

    printf("Enter principal: ");
    if (scanf("%lf", &principal) != 1 || principal < 0) {
        printf("Please enter a non-negative numeric principal.\n");
        return 1;
    }

    printf("Enter annual interest rate (percent): ");
    if (scanf("%lf", &annualRate) != 1 || annualRate < 0) {
        printf("Please enter a non-negative numeric rate.\n");
        return 1;
    }

    printf("Enter time in years: ");
    if (scanf("%lf", &timeYears) != 1 || timeYears < 0) {
        printf("Please enter a non-negative numeric time.\n");
        return 1;
    }

    interest = principal * (annualRate / 100.0) * timeYears;
    totalAmount = principal + interest;

    printf("Simple interest: %.2f\n", interest);
    printf("Total amount: %.2f\n", totalAmount);
    return 0;
}

Save the source as, for example, simple_interest.c, then compile it with a C compiler such as GCC using gcc simple_interest.c -o simple_interest. Run the resulting program from a terminal. Compiler commands can vary by operating system and installed toolchain, but the C calculations themselves are standard.

How the C Calculation Works

The input variables use the type double, which supports fractional values such as a principal of 1250.50, a rate of 4.25%, or a duration of 1.5 years. The program checks each call to scanf: it must successfully read one value, and the value must not be negative. The check reports an error and exits rather than calculating from invalid input.

In the interest expression, annualRate / 100.0 converts a percent into a decimal rate. The decimal constant matters: if both operands of a division were integers, C would perform integer division and could discard the fractional part. Using 100.0 ensures floating-point division. The format specifier %.2f prints a floating-point value rounded for display to two decimal places; it does not change the stored value used in later calculations.

When Simple Interest Applies

Simple interest is a useful model when interest is calculated only on the original principal for the whole period. It is also a common introductory programming exercise because it demonstrates input, arithmetic, variables, validation, and formatted output. A result from this formula is not automatically the same as the amount on a real financial statement: actual terms may use a different accrual method, compounding schedule, or fee structure.

The program assumes the time input is measured in years. For a duration supplied in months, convert months to years before using the formula—for example, 6 months is 6 ÷ 12 = 0.5 years. For days, the appropriate year fraction depends on the convention used in the agreement or calculation; do not assume every calculation treats a year as the same number of days.

Common Mistakes and Limits

  • Entering 5 instead of 0.05 in a different formula: This program expects the annual rate as a percentage, so 5 means 5%. If you change the code to accept a decimal rate, remove the division by 100.
  • Mixing units: A time value of 6 means six years to this program, not six months. Convert the time to years first.
  • Using integer types: int cannot represent fractional inputs. Use double for the rate, time, and principal when decimals are possible.
  • Confusing interest with total amount: The interest alone is interest; principal plus interest is totalAmount.
  • Assuming displayed cents guarantee exact decimal arithmetic: Binary floating-point can represent some decimal fractions only approximately. Formatting rounds the displayed result, which is generally adequate for a small demonstration but may not meet the requirements of production financial software.

Simple interest differs from compound interest. With compounding, earned interest is added to the balance and can itself earn interest in later periods. A familiar compound-growth model is A = P(1 + r/n)nt, where n is the number of compounding periods per year. Use that model only when its compounding assumptions match the situation; it is not a replacement to make silently in a simple-interest program.

Assumptions

  • The entered rate is an annual percentage rate used as a simple annual rate, not a periodically compounded rate.
  • The duration is expressed in years and can be fractional.
  • Interest is calculated on the original principal for the entire duration; there are no interim payments or changes in principal.
  • The example program accepts non-negative numeric inputs and reports monetary output to two decimal places. It does not model fees, taxes, or other contract terms.

Frequently Asked Questions

What is the formula for simple interest in C?
Use interest = principal * (annualRate / 100.0) * timeYears; when the rate is entered as a percentage and time is in years. Add interest to principal to get the total amount.

Why divide the interest rate by 100?
A percentage must be converted to a decimal multiplier. For example, 5% is 0.05, so the code divides the entered value by 100.0.

Can I enter time in months?
Convert months to years first by dividing by 12. For instance, 18 months becomes 1.5 years. Keep the input label clear so users know which unit the program expects.

Why use double instead of int?
double can represent fractional rates, time periods, and principal amounts. An integer type would lose those fractions and can produce incorrect results.

Does this code calculate compound interest?
No. It calculates interest on the original principal only. Compound interest requires a different formula that accounts for how often interest is added to the balance.

This calculator is educational estimate arithmetic; for a real financial transaction, the lender’s disclosure gives the exact figures.

Related

Formula used
in p | Principal | $ | 1000 | 0 | 100000000
in apr | Annual interest rate | % | 5 | 0.01 | 100
in yrs | Time | years | 2 | 0 | 100
out int | Simple interest | $ | p * apr / 100 * yrs | 2
out total | Total amount | $ | p + int | 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 →