Calculate the value of a monthly investment in C#


The magic of an investment with compound interest is that, over time, you get interest on the interest.

For each month this program calculates the interest on the account balance. It then adds the interest and a monthly contribution to the balance and displays the result. (Note that interest is compounded monthly not continuously.)

The following code shows how the program performs its calculations.

// Calculate the interest compounded monthly.
private void btnGo_Click(object sender, EventArgs e)
    // Get the parameters.
    decimal monthly_contribution = decimal.Parse(
        txtMonthlyContribution.Text, NumberStyles.Any);
    int num_months = int.Parse(txtNumMonths.Text);
    decimal interest_rate = decimal.Parse(
        txtInterestRate.Text.Replace("%", "")) / 100;
    interest_rate /= 12;

    // Calculate.
    decimal balance = 0;
    decimal total_interest = 0;
    decimal total_principle = 0;
    for (int i = 1; i <= num_months; i++)
        // Display the month.
        ListViewItem new_item = lvwBalance.Items.Add(i.ToString());

        // Display the interest.
        decimal interest = balance * interest_rate;
        total_interest += interest;

        // Add the contribution.
        balance += monthly_contribution;
        total_principle += monthly_contribution;

        // Display the balance.
        balance += interest;

    // Scroll to the last entry.
    lvwBalance.Items[lvwBalance.Items.Count - 1].EnsureVisible();

The code first gets the input parameters. It passes the parameter NumberStyles.Any to decimal.Parse so it can parse currency values. It also removes the percentage symbol from the interest rate, if it present.

The program then clears its ListView and then loops through the months being studied.

For each month, the code calculates and displays the interest on the current balance. It then adds the current interest to the total interest and displays that.

Next the program adds this month’s contribution to the total balance and the total principle, and displays those values. Finally it displays the new balance.

After it finishes the loop, the code calls the EnsureVisible method for the ListView control’s last entry to scroll to the end of the list.

This kind of investment has two benefits. First it makes you contribute a relatively small amount or principle each month so over time the principle adds up. Second the interest is added to the balance so over time older interest generates more interest. In this example during the final month the accumulated interest is about 30% of the total balance so roughly 30% if the interest is generated by the interest. If you continued this example for 18 years and 1 month, the total interest would exceed the total principle.

Download Example   Follow me on Twitter   RSS feed   Donate

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

2 Responses to Calculate the value of a monthly investment in C#

  1. Pingback: Show compound interest graphically in C# - C# HelperC# Helper

  2. Pingback: Calculate credit payments in C# - C# HelperC# Helper

Comments are closed.