Created
March 30, 2012 15:15
-
-
Save skinp/2252212 to your computer and use it in GitHub Desktop.
Compound interest in awk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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