# Calculates the future value of an investment using
# annual compound interest.
#
# Formula:
# A = P * (1 + r)^t
#
# Where:
# A = final amount
# P = principal (initial investment)
# r = annual interest rate as a decimal
# t = number of years
#
# @param principal [Numeric] Initial amount invested
# @param rate_percent [Numeric] Annual interest rate (e.g., 3.5 for 3.5%)
# @param years [Integer] Number of years money is invested
# @return [Float] Future value after compounding
def compound_interest(principal, rate_percent, years)
raise ArgumentError, "Principal must be non-negative" if principal.negative?
raise ArgumentError, "Years must be non-negative" if years.negative?
rate = rate_percent / 100.0
principal * (1 + rate)**years
end
# Input data
principal = 100_000
rate = 3.5
years = 5
# Perform the calculation
final_amount = compound_interest(principal, rate, years)
interest_earned = final_amount - principal
# Display results
puts "Principal: $#{format('%.2f', principal)}"
puts "Annual Rate: #{rate}%"
puts "Years: #{years}"
puts "Final Amount: $#{format('%.2f', final_amount)}"
puts "Interest Earned: $#{format('%.2f', interest_earned)}"
# run:
#
# Principal: $100000.00
# Annual Rate: 3.5%
# Years: 5
# Final Amount: $118768.63
# Interest Earned: $18768.63
#