# Write a C# Program to Calculate Compound Interest

Do you want to calculate compound interest in C#? In this C# tutorial, I will show you how to write a C# program to calculate compound interest.

To calculate compound interest in C#, you need to use the formula as: The future value (A) is calculated by multiplying the principal amount (P) by one plus the annual interest rate (r) divided by the number of compounding periods per year (n), all raised to the power of the total number of compounding periods, which is the product of the number of compounding periods per year (n) and the number of years (t).

## C# Program to Calculate Compound Interest

Compound interest is the interest calculated on the initial principal and also on the accumulated interest of previous periods. The formula to calculate compound interest is:

Here:

• A is the future value of the investment/loan, including interest.
• P is the principal amount (initial investment).
• r is the annual interest rate (decimal).
• n is the number of times interest is compounded per year.
• t is the time the money is invested or borrowed for, in years.

To write the code, we will use a console application in C#. This application will prompt the user to enter the principal amount, interest rate, the number of times interest is compounded per year and the number of years the money is invested or borrowed.

Here is the complete code to calculate the compound interest in C#:

``````using System;

class CompoundInterestCalculator
{
static void Main(string[] args)
{
// Getting user input
Console.WriteLine("Enter the principal amount:");

Console.WriteLine("Enter the annual interest rate (in percentage):");
double annualInterestRate = Convert.ToDouble(Console.ReadLine()) / 100;

Console.WriteLine("Enter the number of times interest is compounded per year:");

Console.WriteLine("Enter the number of years:");

// Calculating compound interest
double amount = CalculateCompoundInterest(principal, annualInterestRate, compoundFrequency, years);

// Displaying the result
Console.WriteLine(\$"The future value of the investment is: {amount:C2}");
}

static double CalculateCompoundInterest(double principal, double annualInterestRate, int compoundFrequency, int years)
{
return principal * Math.Pow(1 + annualInterestRate / compoundFrequency, compoundFrequency * years);
}
}
``````

After writing the code, run the program. It will prompt you to enter the required values, and after inputting them, it will display the future value of your investment or loan.

You can see the screenshot below.

## How to Calculate Compound Interest in C#

The below program is an enhanced program that calculates not only the total compound interest but also the interest per year; we can modify the program to include a loop that calculates and displays the interest for each year. Here is the complete C# code to calculate compound interest.

``````using System;

class CompoundInterestCalculator
{
static void Main(string[] args)
{
// Getting user input
Console.WriteLine("Enter the principal amount:");

Console.WriteLine("Enter the annual interest rate (in percentage):");
double annualInterestRate = Convert.ToDouble(Console.ReadLine()) / 100;

Console.WriteLine("Enter the number of times interest is compounded per year:");

Console.WriteLine("Enter the number of years:");

// Calculating and displaying compound interest per year
double previousAmount = principal;
for (int year = 1; year <= years; year++)
{
double amount = CalculateCompoundInterest(principal, annualInterestRate, compoundFrequency, year);
double annualInterest = amount - previousAmount;
previousAmount = amount;

Console.WriteLine(\$"Year {year}: Interest = {annualInterest:C2}, Total Amount = {amount:C2}");
}
}

static double CalculateCompoundInterest(double principal, double annualInterestRate, int compoundFrequency, int years)
{
return principal * Math.Pow(1 + annualInterestRate / compoundFrequency, compoundFrequency * years);
}
}
``````

This modified program now includes a `for` loop that iterates through each year, calculates the compound interest for that year, and displays both the interest earned for that year and the total amount accumulated up to that year. This approach provides a detailed yearly breakdown of the compound interest.

You can check the screenshot for the output:

## Conclusion

Writing a C# program to calculate compound interest is not only a great way to understand financial calculations but also an excellent practice for beginners in C#. In this C# tutorial, I have explained how to write a C# program to calculate compound interest.

You may also like: