[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Calculate the value of a monthly investment in C#

[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. lvwBalance.Items.Clear(); 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; new_item.SubItems.Add(interest.ToString("c")); total_interest += interest; new_item.SubItems.Add(total_interest.ToString("c")); // Add the contribution. balance += monthly_contribution; total_principle += monthly_contribution; new_item.SubItems.Add(total_principle.ToString("c")); // Display the balance. balance += interest; new_item.SubItems.Add(balance.ToString("c")); } // 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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.