Created
November 9, 2018 22:36
-
-
Save eliquious/6cf6ee5177454213a95862e0e097e553 to your computer and use it in GitHub Desktop.
Loan calculator and amortization schedule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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