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.






Comments and Discussions

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





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.