# Golang : Compound interest over time example

Tags : golang compound-interest formula read-float read-integer

It is time for me to do something "lighter" now.... such as investing rather than heavy duty programming ..... so that I get to spend more quality time with friends and family members. In investing, the open secret is to compound your money over time.

In fact, according to Albert Einstein :

“Compound interest is the eighth wonder of the world. He who understands it, earns it ... he who doesn't ... pays it.”

The common compound formula that most people understand is how to calculate compound interest over certain time. There are other compound formulas to calculate other types of cash flow(stream of money or earnings), but we will just use the basic compound interest formula.

Below is an example of how to calculate compound interest in Golang. I've tested the function with the given examples found in Wikipedia and the result matched. It should be accurate as far as my own mathematical knowledge goes.

Here you go!

```
package main
import (
"fmt"
"math"
)
// calculate the compound interest with formula from Wikipedia
// https://en.wikipedia.org/wiki/Compound_interest#Calculation_of_compound_interest
func compoundInterest(principal float64, interestRate float64, frequencyPerYear float64, overYears int) float64 {
// (1 + interestRate / frequencyPerYear)
block := 1 + (interestRate / float64(frequencyPerYear))
// frequencyPerYear * overYears
exponent := frequencyPerYear * float64(overYears)
compoundInterest := principal * math.Pow(block, float64(exponent))
return compoundInterest
}
func main() {
wikiExample := compoundInterest(1500, 0.043, 4, 6)
fmt.Println(wikiExample)
var p, rate, freq float64
var time int
fmt.Println("Enter the principal : ")
fmt.Scanf("%f", &p)
fmt.Println("Enter the interest rate : ")
fmt.Scanf("%f", &rate)
fmt.Println("Enter the compounding frequency per year : ")
fmt.Println("[1 for yearly, 4 for quarterly and 12 for monthly]")
fmt.Scanf("%f", &freq)
fmt.Println("Enter the duration in years : ")
fmt.Scanf("%d", &time)
result := compoundInterest(p, rate, freq, time)
fmt.Println(result)
}
```

Sample output:

1938.8368221341061

Enter the principal :

1500

Enter the interest rate :

0.043

Enter the compounding frequency per year :

[1 for yearly, 4 for quarterly and 12 for monthly]

0.5 <--- n = 1/2 = 0.5 (the interest is compounded every two years)

Enter the duration in years :

6

1921.2360840000001

Happy coding and may the wealth be with you!

**References:**

https://www.socketloop.com/tutorials/golang-how-to-read-float-value-from-standard-input

https://en.wikipedia.org/wiki/Compound*interest#Calculation*of*compound*interest

https://www.socketloop.com/tutorials/golang-how-to-read-integer-value-from-standard-input

http://www.goodreads.com/quotes/76863-compound-interest-is-the-eighth-wonder-of-the-world-he

Tags : golang compound-interest formula read-float read-integer

##### By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.

#### Advertisement

#### Tutorials

+4k Golang : Get all local users and print out their home directory, description and group id

+4.9k Golang : Gorilla web tool kit secure cookie example

+6.1k Golang : Web routing/multiplex example

+5.6k Golang : Read file and convert content to string

+21.6k Golang : Marshal and unmarshal json.RawMessage struct example

+7k Golang : Basic authentication with .htpasswd file

+15.6k Golang : How to create new XML file ?

+2.4k List of Golang XML tutorials

+5.7k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?

+1.8k Unix/Linux : How to pipe/save output of a command to file?

+5.6k Golang : Check if an integer is negative or positive

+11.8k Golang : bufio.NewReader.ReadLine to read file line by line