Python

Numbers

def **compound_interest**(principal, rate, times_per_year, years):
*# (1 + r/n)*
body = 1 + (rate / times_per_year)
*# nt*
exponent = times_per_year * years
*# P(1 + r/n)^nt*
return principal * pow(body, exponent)
*# Compute 0.43% quarterly compound interest for 6 years.*
result = compound_interest(1500, 0.043, 4, 6)
*# Write result.*
print(result)
print()
*# Compute 20% compound interest yearly, quarterly and monthly.*
print(compound_interest(1000, 0.2, 1, 10))
print(compound_interest(1000, 0.2, 4, 10))
print(compound_interest(1000, 0.2, 12, 10))1938.8368221341054
6191.7364223999975
7039.988712124658
7268.254992160187

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

This page was last updated on May 23, 2023 (edit).