Skip to content

Instantly share code, notes, and snippets.

@eliquious
Created November 9, 2018 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliquious/6cf6ee5177454213a95862e0e097e553 to your computer and use it in GitHub Desktop.
Save eliquious/6cf6ee5177454213a95862e0e097e553 to your computer and use it in GitHub Desktop.
Loan calculator and amortization schedule
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("Loan calculator")
P := 40000.
APR := 5.5
years := 15.
periods := years * 12.
r := APR / 100. / 12.
rP := r * P
rN := math.Pow(1+r, -float64(periods))
c := (rP / (1 - rN))
fmt.Printf("Payment: %.2f\n", c)
fmt.Printf("Total paid: %.2f\n", c*periods)
fmt.Printf("\n Amortization Schedule\n\n")
fmt.Printf(" Mth %10s %10s %10s\n", "Total", "Principal", "Interest")
fmt.Println("--------------------------------------")
for i := 1; i < int(periods) + 1; i++ {
interest := (P * r - c) * (math.Pow(1+r, float64(i)) - 1)/r + c*float64(i)
principal := c*float64(i) - interest
fmt.Printf("[%03d] %10.2f %10.2f %10.2f\n", i, c*float64(i), principal, interest)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment