package main
import (
"fmt"
"math"
)
// calculateCompoundInterest computes the compound interest
// given principal, annual rate (in %), and time in years.
func calculateCompoundInterest(principal, rate, years float64) float64 {
// Validate input: all values must be non-negative
if principal < 0 || rate < 0 || years < 0 {
fmt.Println("Error: Principal, rate, and years must be non-negative values.")
return -1
}
// Calculate total amount using the compound interest formula
amount := principal * math.Pow(1+rate/100, years)
// Return compound interest (total amount - principal)
return amount - principal
}
func main() {
principal := 100000.0
rate := 3.5
years := 5.0
compoundInterest := calculateCompoundInterest(principal, rate, years)
if compoundInterest >= 0 {
fmt.Printf("Principal Amount: %.2f\n", principal)
fmt.Printf("Annual Interest Rate: %.2f%%\n", rate)
fmt.Printf("Years: %.2f\n", years)
fmt.Printf("Compound Interest: %.2f\n", compoundInterest)
fmt.Printf("Total Amount: %.2f\n", principal+compoundInterest)
}
}
/*
run:
Principal Amount: 100000.00
Annual Interest Rate: 3.50%
Years: 5.00
Compound Interest: 18768.63
Total Amount: 118768.63
*/