
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# program to calculate compound interest
Compound interest is interest where the interest is calculated by adding the interest that has already been calculated in a previous time period. In this article, we are going to discuss how we can calculate compound interest using C#.
What is Compound Interest?
Compound Interest is the interest that calculates interest on the initial principal and also includes the interest of the previous period. In compound interest, the previous interest is included in the current interest, causing the interest to grow in an exponential manner.
Example
- Input: principal = 20,000, rate = 10, years = 3
- Output: Compound Interest = 6620
- Input: principal = 3,000, rate = 5, years = 2
- Output: Compound Interest = 307.5
Using Formula-Based Approach
We are given the principal amount, rate (r), and time (t) in years. We will use the formula to calculate compound interest. First, we calculate the amount using the formula:
Amount = Principal à (1 + Rate/100)time
Compound Interest = Amount - Principal
Steps for Implementation
- We will take input for the principal, rate, and time period in years.
- We will first calculate the amount using the above formula.
- Then, we calculate the compound interest by subtracting the principal from the amount.
Implementation Code
using System; class Program { static void Main(string[] args) { // Take the Principal amount double principal = 20000; // Annual rate of interest double rate = 10; // Time in years double time = 3; // Calculate the amount double amount = principal * Math.Pow((1 + rate / 100), time); // Calculate the compound interest double compound_Interest = amount - principal; // Output Console.WriteLine("The Compound Interest is: {0}", compound_Interest); } }
Output:
The Compound Interest is: 6620.00000000001
Time Complexity: O(1)
Space Complexity: O(1)
Using Iterative Approach
Instead of directly using the Math.Pow()
function, we can use a loop to calculate the compound interest. We traverse the time period year by year to calculate the accumulation of interest. This method is not preferable because it will increase time complexity due to iteration.
Steps for Implementation
- First, we will take input for the principal, rate, and time period in years.
- Now, we iteratively calculate the amount year by year using a for loop.
- Then, we calculate the compound interest by subtracting the principal from the amount.
Implementation Code
using System; class Program { static void Main(string[] args) { // Input values double principal = 20000; double rate = 10; double time = 3; // Iteratively calculate amount double amount = principal; for (int i = 0; i < time; i++) { amount += amount * (rate / 100); } // Calculate compound interest double compoundInterest = amount - principal; // Output the result Console.WriteLine($"Compound Interest is: {compoundInterest}"); } }
Output:
Compound Interest is: 6620
Time Complexity: O(n), number of iterations
Space Complexity: O(1)