[C# Helper]
Index Books FAQ Contact About Rod
[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]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

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

Title: 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) { lstResults.Items.Clear(); 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" + balance.ToString("C")); } }

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.

Download the example to experiment with it and to see additional details.

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