0
0
CProgramBeginner · 2 min read

C Program to Calculate Compound Interest with Example

A C program to calculate compound interest uses the formula CI = P * pow(1 + R/100, T) - P where P is principal, R is rate, and T is time; you can implement it by reading inputs and printing the calculated compound interest.
📋

Examples

InputPrincipal = 1000, Rate = 5, Time = 2
OutputCompound Interest = 102.50
InputPrincipal = 1500, Rate = 4.3, Time = 6
OutputCompound Interest = 438.84
InputPrincipal = 2000, Rate = 0, Time = 5
OutputCompound Interest = 0.00
🧠

How to Think About It

To calculate compound interest, first understand that interest is added to the principal each period, so the amount grows exponentially. Use the formula Amount = Principal * (1 + Rate/100)^Time. Then subtract the principal from the amount to get the compound interest. This approach models how money grows with interest over time.
📐

Algorithm

1
Get input values for principal, rate of interest, and time period.
2
Calculate the amount using the formula: principal * (1 + rate/100) raised to the power of time.
3
Subtract the principal from the amount to get the compound interest.
4
Display the compound interest.
💻

Code

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

int main() {
    double principal, rate, time, amount, compound_interest;
    printf("Enter principal, rate and time: ");
    scanf("%lf %lf %lf", &principal, &rate, &time);
    amount = principal * pow(1 + rate / 100, time);
    compound_interest = amount - principal;
    printf("Compound Interest = %.2lf\n", compound_interest);
    return 0;
}
Output
Enter principal, rate and time: 1000 5 2 Compound Interest = 102.50
🔍

Dry Run

Let's trace the example where principal=1000, rate=5, time=2 through the code

1

Input values

principal = 1000, rate = 5, time = 2

2

Calculate amount

amount = 1000 * pow(1 + 5/100, 2) = 1000 * pow(1.05, 2) = 1000 * 1.1025 = 1102.5

3

Calculate compound interest

compound_interest = 1102.5 - 1000 = 102.5

4

Print result

Output: Compound Interest = 102.50

StepCalculationValue
Calculate amount1000 * (1 + 5/100)^21102.5
Compound interest1102.5 - 1000102.5
💡

Why This Works

Step 1: Reading inputs

The program reads principal, rate, and time from the user to know the starting amount, interest rate, and duration.

Step 2: Calculating amount

It calculates the total amount after interest using pow(1 + rate/100, time) which raises the growth factor to the power of time.

Step 3: Finding compound interest

The compound interest is the total amount minus the original principal, showing how much extra money was earned.

Step 4: Displaying result

Finally, the program prints the compound interest rounded to two decimal places for clarity.

🔄

Alternative Approaches

Using a loop to calculate compound interest
c
#include <stdio.h>

int main() {
    double principal, rate, time, amount;
    int i;
    printf("Enter principal, rate and time: ");
    scanf("%lf %lf %lf", &principal, &rate, &time);
    amount = principal;
    for(i = 0; i < (int)time; i++) {
        amount += amount * rate / 100;
    }
    printf("Compound Interest = %.2lf\n", amount - principal);
    return 0;
}
This method uses a loop to add interest each year, which is easier to understand but less efficient for large time values.
Using float instead of double
c
#include <stdio.h>
#include <math.h>

int main() {
    float principal, rate, time, amount, ci;
    printf("Enter principal, rate and time: ");
    scanf("%f %f %f", &principal, &rate, &time);
    amount = principal * powf(1 + rate / 100, time);
    ci = amount - principal;
    printf("Compound Interest = %.2f\n", ci);
    return 0;
}
Using float saves memory but reduces precision, which may be acceptable for simple calculations.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation uses a constant number of operations including a power function call, so it runs in constant time.

Space Complexity

Only a few variables are used to store inputs and results, so space usage is constant.

Which Approach is Fastest?

Using the pow function is faster and cleaner than looping, especially for fractional or large time values.

ApproachTimeSpaceBest For
Using pow functionO(1)O(1)Fast and precise calculations
Using loopO(n)O(1)Simple logic, integer time periods
Using floatO(1)O(1)Memory-limited environments with less precision
💡
Use pow from math.h to easily calculate powers for compound interest.
⚠️
Beginners often forget to divide the rate by 100, causing incorrect interest calculations.