Calculate compound interest over time in C#

This example shows how to calculate compound interest over a time period. Enter the principle amount, interest rate, and number of years in the TextBoxes. When you click Calculate, the program uses the following code to display your balance for the following years.

// Calculate and display interest for the following years.
private void btnCalculate_Click(object sender, EventArgs e)

    double principle = double.Parse(txtPrinciple.Text);
    double interestRate = double.Parse(txtInterestRate.Text);
    int numYears = int.Parse(txtNumYears.Text);
    for (int i = 1; i <= numYears; i++)
        double balance = principle * Math.Pow(1 + interestRate, i);
        lstResults.Items.Add("Year " + i.ToString() + "\t" +

The program simply loops through the years, evaluating the compound interest formula:

    balance = principle * Math.Pow(1 + interestRate, i)

This is the simple compound interest formula so interest is calculated only once per year.

Interesting tidbit: To estimate how long it will take to double your money, you can use the “Rule of 72.” Divide the interest rate into 72 and the result tells you roughly how many years it will take to double your money. For example, at 7.2% it’ll take about 10 years. It’s a pretty decent estimate.

This entry was posted in algorithms, mathematics and tagged , , , , , , , , , . Bookmark the permalink.

5 Responses to Calculate compound interest over time in C#

  1. i want to add compound interest in principal amount after every 6 month how this possible in c#.

    • RodStephens says:

      This example uses the formula for continuously compounded interest. If you only want to compound it every 6 months, just loop over the 6 month periods. Multiply the current balance by the interest rate divided by two (because you’re taking interest over 6 months not the full year) and add it to the balance.

      (Instead of compounding every 6 months, you could do it every month. Or every week. Or day, or hour, or second, etc. The continuous compounding formula is what you get when you take this to the limit and compound the interest at infinitely small intervals.)

  2. Rajeev More says:

    i want to add interest only if month is April of every year. what should i do?

    • RodStephens says:

      You should probably add non-compounded interest every April. Calculate the fraction of the year that has elapsed since the last time period started. For the first April, that may not be a full year if the deposit/loan didn’t start in the previous April.

      Then multiply by the interest rate times the fraction of the year that has elapsed and add it as interest.

  3. Pingback: Calculate the present value of a future amount of money in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *