C# - Calculate the Compound Interest

Here, we are going to learn how to calculate the compound interest in C#? By Nidhi Last updated : April 15, 2023

Compound Interest

Compound interest is the addition of interest to the principal sum of a loan or deposit, or in other words, interest on principal plus interest. [Source]

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.

Here, we will calculate the compound interest.

C# program to calculate the compound interest

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

//C# - 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.

C# Basic Programs »


Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.