public static void Main()
float initialInvestment = 20000;
float yearlyInvestmentRatio = 1.3f;
float yearlyGains = 0.25f;
float finalBalancePayingTaxesRightAway = 0;
float finalBalancePayingTaxesAtTheEnd = 0;
for (int experiment = 0; experiment < 2; experiment++)
bool payTaxesRightAway = experiment == 0;
Console.WriteLine("Pay taxes right away:");
Console.WriteLine("Pay taxes at the end:");
float totalInvestments = 0;
float currentInvestment = initialInvestment;
float currentBalance = 0;
for (int year = 0; year < numberOfYears; year++)
float gains = yearlyGains * currentBalance;
float newBalance = currentBalance + gains;
float taxes = taxRate1 * gains;
Console.WriteLine($"Year {year} - Current balance: {currentBalance.ToString("n0")}. Gains: {gains.ToString("n0")}. Taxes: {taxes.ToString("n0")}. New balance: {newBalance.ToString("n0")}. Investment: {currentInvestment.ToString("n0")}");
Console.WriteLine($"Year {year} - Current balance: {currentBalance.ToString("n0")}. Gains: {gains.ToString("n0")}. New balance: {newBalance.ToString("n0")}. Investment: {currentInvestment.ToString("n0")}");
newBalance += currentInvestment;
totalInvestments += currentInvestment;
currentBalance = newBalance;
currentInvestment *= yearlyInvestmentRatio;
float finalTaxes = taxRate2 * gainsToTax;
currentBalance -= finalTaxes;
Console.WriteLine($"Final taxes: {finalTaxes.ToString("n0")}. Final balance: {currentBalance.ToString("n0")}");
finalBalancePayingTaxesRightAway = currentBalance;
finalBalancePayingTaxesAtTheEnd = currentBalance;
float netGains = currentBalance - totalInvestments;
Console.WriteLine($"Final balance: {currentBalance.ToString("n0")}. Total investments: {totalInvestments.ToString("n0")}. Net gains: {netGains.ToString("n0")}");
if (finalBalancePayingTaxesRightAway > finalBalancePayingTaxesAtTheEnd) {
Console.WriteLine($"You save {(finalBalancePayingTaxesRightAway - finalBalancePayingTaxesAtTheEnd).ToString("n0")} by paying taxes right away");
Console.WriteLine($"You save {(finalBalancePayingTaxesAtTheEnd - finalBalancePayingTaxesRightAway).ToString("n0")} by paying taxes at the end");