Last active
February 3, 2020 21:13
-
-
Save 9cardinals/7313429 to your computer and use it in GitHub Desktop.
simple compound interest calculator using math operations in ruby
This file contains hidden or 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
# principal amount | |
p = 10_000 | |
# annual rate of interest | |
r = 0.05 | |
# number of years | |
t = 5 | |
# number of times it is compounded | |
n = 12 | |
# amount accumulated | |
a = p * (1 + r/n) ** (n*t) | |
p "After #{t} years I'll have #{a} dollars!" | |
# slightly more complex version | |
# p = principal amount | |
# r = annual rate of interest | |
# t = number of years | |
# n = number of times it is compounded | |
def compound_interest(name, p, r, t, n) | |
a = p * (1 + r/n) ** (n*t) | |
"After #{n} years #{name} will have #{a} dollars!" | |
end | |
p compound_interest("Bob", 100, 0.05, 40, 12) | |
p compound_interest("Joe", 250, 0.06, 50, 12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment