C Program to Calculate Compound Interest with Example
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
How to Think About It
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
Code
#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; }
Dry Run
Let's trace the example where principal=1000, rate=5, time=2 through the code
Input values
principal = 1000, rate = 5, time = 2
Calculate amount
amount = 1000 * pow(1 + 5/100, 2) = 1000 * pow(1.05, 2) = 1000 * 1.1025 = 1102.5
Calculate compound interest
compound_interest = 1102.5 - 1000 = 102.5
Print result
Output: Compound Interest = 102.50
| Step | Calculation | Value |
|---|---|---|
| Calculate amount | 1000 * (1 + 5/100)^2 | 1102.5 |
| Compound interest | 1102.5 - 1000 | 102.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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Using pow function | O(1) | O(1) | Fast and precise calculations |
| Using loop | O(n) | O(1) | Simple logic, integer time periods |
| Using float | O(1) | O(1) | Memory-limited environments with less precision |
pow from math.h to easily calculate powers for compound interest.