Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to calculate compound interest
Compound interest is interest calculated on the initial principal amount plus all previously earned interest. Unlike simple interest, compound interest grows exponentially because the interest itself earns interest in subsequent periods. In this article, we will explore how to calculate compound interest using C#.
What is Compound Interest?
Compound Interest is the interest that calculates interest on both the initial principal and the accumulated interest from previous periods. This compounding effect causes the investment to grow at an accelerating rate over time.
Formula
The compound interest formula is
Amount = Principal × (1 + Rate/100)^Time Compound Interest = Amount - Principal
Where
- Principal: Initial amount invested
- Rate: Annual interest rate (as percentage)
- Time: Number of years
Using Formula-Based Approach
The most efficient way to calculate compound interest is using the mathematical formula with Math.Pow() function
using System;
class Program {
static void Main(string[] args) {
// Input values
double principal = 20000;
double rate = 10;
double time = 3;
// Calculate amount using compound interest formula
double amount = principal * Math.Pow((1 + rate / 100), time);
// Calculate compound interest
double compoundInterest = amount - principal;
// Display results
Console.WriteLine("Principal Amount: ${0}", principal);
Console.WriteLine("Rate of Interest: {0}%", rate);
Console.WriteLine("Time Period: {0} years", time);
Console.WriteLine("Final Amount: ${0:F2}", amount);
Console.WriteLine("Compound Interest: ${0:F2}", compoundInterest);
}
}
The output of the above code is
Principal Amount: $20000 Rate of Interest: 10% Time Period: 3 years Final Amount: $26620.00 Compound Interest: $6620.00
Using Iterative Approach
We can also calculate compound interest iteratively by applying the interest year by year. This approach helps understand how interest compounds over time
using System;
class Program {
static void Main(string[] args) {
// Input values
double principal = 3000;
double rate = 5;
int time = 2;
double amount = principal;
Console.WriteLine("Year-by-year breakdown:");
Console.WriteLine("Year 0: ${0:F2}", amount);
// Calculate interest for each year
for (int year = 1; year <= time; year++) {
double yearlyInterest = amount * (rate / 100);
amount += yearlyInterest;
Console.WriteLine("Year {0}: ${1:F2} (Interest: ${2:F2})",
year, amount, yearlyInterest);
}
double compoundInterest = amount - principal;
Console.WriteLine("\nFinal Compound Interest: ${0:F2}", compoundInterest);
}
}
The output of the above code is
Year-by-year breakdown: Year 0: $3000.00 Year 1: $3150.00 (Interest: $150.00) Year 2: $3307.50 (Interest: $157.50) Final Compound Interest: $307.50
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Use Case |
|---|---|---|---|
| Formula-Based | O(1) | O(1) | Quick calculation, large time periods |
| Iterative | O(n) | O(1) | Understanding growth, detailed breakdown |
Conclusion
Compound interest can be calculated efficiently using the mathematical formula with Math.Pow(), or iteratively for a detailed year-by-year breakdown. The formula-based approach is preferred for performance, while the iterative method helps visualize how interest compounds over time.
