Balance after yearly compounding
An account reinvests its interest once a year. Report the balance after a whole number of years.
- Each year the balance gains floor(balance × rate / 100) before the year ends.
- Zero years returns the untouched principal.
- Interest is credited in whole minor units, rounding down each year.
compound_balance(principal: int, rate_percent: int, years: int) → int
Where you start
def compound_balance(principal: int, rate_percent: int, years: int) -> int:
Worked examples
| Call | Result |
|---|---|
compound_balance(1000, 10, 2) | 1210 |
compound_balance(100, 5, 3) | 115 |
compound_balance(5000, 0, 5) | 5000 |
compound_balance(0, 10, 5) | 0 |
Hint
Loop the years, adding the floored interest each pass.
Reference solution in Python
def compound_balance(principal: int, rate_percent: int, years: int) -> int:
balance = principal
for _ in range(years):
balance += balance * rate_percent // 100
return balance