Calculate the future value of a monthly investment with interest in C#

The magic of compound interest is that, over time, you get interest on the interest

For each month the 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;
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"));

// Add the contribution.
balance += monthly_contribution;

// Display the balance.
balance += interest;
new_item.SubItems.Add(balance.ToString("c"));
}

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

  

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.