Title: Compare a savings account to a 401(k) 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 being 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 than you paid when you added money to the plan because you may be earning less income. 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.
// Compare regular and 401k account balances.
private void btnGo_Click(object sender, EventArgs e)
decimal annual_contribution = decimal.Parse(
decimal tax_rate =
NumberStyles.Any) / 100;
if (tax_rate >= 1) tax_rate /= 100;
decimal interest_rate =
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;
for (int year = 0; year < num_years; year++)
ListViewItem lv_item = new ListViewItem(year.ToString());
// Bank balance += interest + contribution, minus taxes.
balance_bank = balance_bank +
(1 - tax_rate) *
(balance_bank * interest_rate +
// 401(k) balance += interest + contribution.
balance_401k = balance_401k +
(balance_401k * interest_rate +
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.
As you can see from the picture above, the 401(k) lets you keep a lot more money than a plain savings account. A lot of the difference comes from not paying taxes on the interest over a long period of time. That means if the interest rate is low (as rates are now), the difference is smaller because you're not saving taxes on as much money.
Download the example to experiment with it and to see additional details.