Golang example How to calculate future compound interest

It is a short tutorial about how to calculate compound interest in the go language.

Golang Compound Interest example program

Compound interest is used in the calculating value of an amount in investment and finance domains.

Compound interest is an interest applied to the principal, the period for the period. In mathematical terms, Compound interest has a formula as seen below.

Future Compound Interest Amount = principal × ((interest rate/100)+1) power of number
Interest Amount = Future Compound Interest Amount - Amount
  • principal: principal amount
  • Interest rate: It is a percentage value (%)
  • the number is years in a period

Let’s write a program to calculate compound interest in the go language In a program code,

  • User enters an input of principal amount, interest, and Period from the console
  • save all these values in a temporary variable
  • Calculate future amount and compound interest amount using the above formula
  • Finally, print the result

Here is the golang program code to calculate simple interest.

package main

import (
    "fmt"
)

func main() {
    var principal, interest, period, total compountInterest float64;
    fmt.Print("Please enter principal amount: ")
    fmt.Scanln( & principal)
    fmt.Print("Please enter Interest Rate: ")
    fmt.Scanln( & interest)

    fmt.Print("Please enter period: ")
    fmt.Scanln( & period)

    futureAmount = principal * (math.Pow((1 + interest / 100), period))
    compountInterest = futureAmount - principal

    fmt.Println("\n Compound Interest  Amount: ", compountInterest)
    fmt.Println("\n Total  Future Amount: ", futureAmount)


}

Output:

Please enter principal amount: 10000
Please enter Interest Rate: 24
Please enter period: 1
Compound Interest  Amount: 72850
Total  Future Amount: 1.797010299914431e+61

This program takes an input - principal, rate of interest, and period in years and stores these values in a variable.

Calculated the future and compound interest and returns the total amount.

Conclusion

In this example, You learned, How to calculate compound interest principal amount in the Go language.