Skip to content

Instantly share code, notes, and snippets.

@skinp
Created March 30, 2012 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skinp/2252212 to your computer and use it in GitHub Desktop.
Save skinp/2252212 to your computer and use it in GitHub Desktop.
Compound interest in awk
#!/bin/awk
# Input: "Initial_cash interest_rate number_of_years yearly_deposit"
{
initial_amount = $1
interest_rate = $2
number_of_years = $3
yearly_deposit = $4
total = initial_amount
current_year = 1
while (current_year <= number_of_years) {
total = total * (1 + interest_rate) + yearly_deposit
current_year += 1
}
printf("At a rate of %.2f, you will have %.2f $ after %d years for an initial deposit of %d $ if you add %d $ per year\n",
interest_rate, total, number_of_years, initial_amount, yearly_deposit)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment