Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 4

Simple interest and compound interest

CSimple numeric problemsJNTUH R22 · CS108ES

Aim

To write a C program that reads the principal, the rate of interest per year and the time in years, and finds the simple interest and the compound interest (compounded once a year).

Algorithm

  1. Read the principal P, the rate R (percent per year) and the time T (years).
  2. If any value is negative, print a message and stop.
  3. Find the simple interest SI = (P × T × R) / 100.
  4. Find the amount with compound interest A = P × (1 + R/100)^T.
  5. Find the compound interest CI = A − P.
  6. Print SI, A and CI.

Program

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double principal, rate, years;
    printf("Enter principal amount: ");
    if (scanf("%lf", &principal) != 1) {
        printf("Invalid input\n");
        return 1;
    }
    printf("Enter rate of interest (%% per year): ");
    if (scanf("%lf", &rate) != 1) {
        printf("Invalid input\n");
        return 1;
    }
    printf("Enter time (years): ");
    if (scanf("%lf", &years) != 1) {
        printf("Invalid input\n");
        return 1;
    }
    if (principal < 0 || rate < 0 || years < 0) {
        printf("Values cannot be negative\n");
        return 1;
    }

    double simple = principal * years * rate / 100;
    double amount = principal * pow(1 + rate / 100, years);
    double compound = amount - principal;

    printf("\nSimple interest   = %.2f\n", simple);
    printf("Amount (compound) = %.2f\n", amount);
    printf("Compound interest = %.2f\n", compound);
    return 0;
}

Output

Rs 10000 at 5% for 2 years

Output 1
Enter principal amount: 10000
Enter rate of interest (% per year): 5
Enter time (years): 2

Simple interest   = 1000.00
Amount (compound) = 11025.00
Compound interest = 1025.00

Rs 5000 at 8.5% for 3 years

Output 2
Enter principal amount: 5000
Enter rate of interest (% per year): 8.5
Enter time (years): 3

Simple interest   = 1275.00
Amount (compound) = 6386.45
Compound interest = 1386.45

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

Check of the first run by hand: SI = 10000 × 2 × 5 / 100 = 1000. A = 10000 × 1.05² = 10000 × 1.1025 = 11025, so CI = 11025 − 10000 = 1025.

Compound interest is more than simple interest because each year's interest is added to the principal and earns interest in the following years. If interest is compounded n times a year, the formula becomes A = P × (1 + R/(100n))^(nT).

pow is declared in math.h; on Linux compile with -lm.

Common mistakes

  • Writing pow(1 + rate / 100, years) with rate declared as int. Then rate / 100 is integer division and gives 0 for any rate below 100. Use double (or write rate / 100.0).

  • Printing the amount as the compound interest. The interest is the amount minus the principal: CI = A − P.

  • Using ^ for power, as in (1 + r) ^ t. In C ^ is bitwise XOR; use pow from math.h.

  • Reading double values with %f in scanf. A double needs %lf.

Viva questions

Try to answer each one aloud before you open it.

  • What is the formula for simple interest?

    SI = (P × T × R) / 100, where P is the principal, T the time in years and R the rate of interest in percent per year.

  • What is the formula for compound interest?

    The amount is A = P × (1 + R/100)^T when interest is added once a year, and the compound interest is CI = A − P.

  • Why is compound interest larger than simple interest for more than one year?

    In compound interest, the interest of each year is added to the principal, so the next year's interest is calculated on a bigger amount. Simple interest is always calculated on the original principal.

  • Why can't we use ^ for power in C?

    ^ is the bitwise XOR operator in C. Powers are found with the pow function from math.h, or with a loop.

  • What does pow return?

    pow(x, y) returns x raised to the power y as a double.

Learn the concepts

All Programming for Problem Solving Lab experiments →