Skip to content

Commit 993a50a

Browse files
committed
Add Ruby snippet for calculating compound interest
1 parent 92c408c commit 993a50a

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Calculate Compound Interest
3+
description: Calculates compound interest for a given principal amount, rate, and time period.
4+
author: ACR1209
5+
contributors: axorax
6+
tags: ruby,math,compound interest,finance
7+
---
8+
9+
```ruby
10+
def compound_interest(principal, rate, time, n = 1)
11+
principal * (1 + rate / n) ** (n * time)
12+
end
13+
14+
# Usage:
15+
puts compound_interest(1000, 0.05, 5) # Output: 1276.2815625000003
16+
puts compound_interest(1000, 0.05, 5, 12) # Output: 1283.3586785035118
17+
```

0 commit comments

Comments
 (0)