Roughly compare the savings a normal bank account would give versus a 401(k) (a US tax thing) in C#

Important Note: I am not a tax or investment professional. I don't even pretend to understand this stuff. This is a very simple tool for playing with numbers (it doesn't even compound continuously) and I don't vouch for its correctness. It should in no way be taken for investment advice. What, are you crazy???

That said...

A 401(k) retirement plan lets you save money tax deferred. As I understand it, that means you don't pay tax on that money before you put it into the plan and any interest the money accrues is not taxed. When you remove money from the plan, you pay taxes on it, although you may be paying at a lower tax rate because you may be earning less. There are also penalties if you withdraw money before retirement age (which will probably be around 140 by the time I retire).

Enter the annual contribution you would make to the plan, your income tax rate, the annual interest rate you expect to earn, and the number of years you want to examine. When you click Go, the following code executes.

The program uses the following code to compare normal and 401(k) results.

// Compare regular and 401k account balances.
private void btnGo_Click(object sender, EventArgs e)
{
decimal annual_contribution =
decimal.Parse(txtAnnualContribution.Text, NumberStyles.Any);

decimal tax_rate =
decimal.Parse(txtTaxRate.Text.Replace("%", ""),
NumberStyles.Any) / 100;
if (tax_rate >= 1) tax_rate /= 100;

decimal interest_rate =
decimal.Parse(txtInterestRate.Text.Replace("%", ""),
NumberStyles.Any) / 100;
if (interest_rate >= 1) interest_rate /= 100;

int num_years = int.Parse(txtYears.Text);

decimal balance_bank = 0;
decimal balance_401k = 0;
lvwResults.Items.Clear();
for (int year = 0; year < num_years; year++)
{
ListViewItem lv_item = new ListViewItem(year.ToString());
lvwResults.Items.Add(lv_item);
lv_item.SubItems.Add(balance_bank.ToString("C"));
lv_item.SubItems.Add(balance_401k.ToString("C"));

// Bank balance += interest + contribution, minus taxes.
balance_bank = balance_bank +
(1 - tax_rate) * (balance_bank * interest_rate + annual_contribution);

// 401(k) balance += interest + contribution.
balance_401k = balance_401k +
(balance_401k * interest_rate + annual_contribution);
}
}

The code gets your inputs. It then loops through the years calculating balances for a normal bank account and for a 401(k).

For the bank account, the code calculates your interest for the previous year, adds that to your contribution, subtracts taxes from both, and adds the result to the account balance.

For the 401(k), the calculation is the same except the program does not subtract taxes.

  

 

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.