Home » .Net » C# programs

# C# program to calculate the compound interest

Here, we are going to learn **how to calculate the compound interest in C#?**

Submitted by Nidhi, on September 22, 2020

Here, we will calculate the compound interest.

**Note:** **Compound interest** is the interest on a loan, which is calculated based on both the initial principal and the accumulated interest from previous periods.

**Program:**

The source code to calculate the compound interest is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to calculate the compound interest. using System; class Interest { static void CalculateCompoundInterest(double amount, double roi, int years, int annualCompound) { double result = 0; int loop = 0; for (loop = 1; loop <= years; loop++) { result = amount * Math.Pow((1 + roi / annualCompound), (annualCompound * loop)); Console.WriteLine("Your amount after {0} Year " + ": {1}", loop, result); } } private static void Main() { int years = 0; int annualCompound = 0; double roi = 0; double amount = 0; Console.Write("Enter the amount : "); amount = double.Parse(Console.ReadLine()); Console.Write("Enter the rate of interest : "); roi = double.Parse(Console.ReadLine()) / 100; Console.Write("Enter the total number of years : "); years = int.Parse(Console.ReadLine()); Console.Write("Compounding frequency : "); annualCompound = int.Parse(Console.ReadLine()); CalculateCompoundInterest(amount, roi, years, annualCompound); } }

**Output:**

Enter the amount : 2500 Enter the rate of interest : 7.5 Enter the total number of years : 2 Compounding frequency : 2 Your amount after 1 Year : 2691.015625 Your amount after 2 Year : 2896.62603759766 Press any key to continue . . .

**Explanation:**

Here, we create a class *Interest* that contains two static methods *CalculateCompoundInterest()* and *Main()*. The *CalculateCompoundInterest()* method calculates the compound interest according to the standard calculation method and prints the amount year wise on the console screen.

The *Main()* method is the entry point for the program, here we read the values from the user and passed to the *CacluateCompoundInterest()* method and print the amount year wise.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.