In this post, we will learn how to write a program to calculate compound interest using the Go Programming language.

Compound interest is the interest calculated on the principal amount and the interest accumulated over the previous period.

Compound interest is calculated using the following formula:

- Compound Interest Amount = principal x (1 + rate/100)
^{time} - Compound Interest = Compound Interest Amount – Principal Amount

The below Golang program allows the user to enter the principal amount, rate of interest and time period (in years). Then it uses the compound interest formula to calculate the compound interest.

**Go Program to calculate Compound Interest**

package main import ( "fmt" "math" ) func main() { var principal, rate, time, CI, amount float64 fmt.Print("Enter the principal amount: ") fmt.Scanln(&principal) fmt.Print("Enter the interest rate: ") fmt.Scanln(&rate) fmt.Print("Enter the time period: ") fmt.Scanln(&time) amount = principal * (math.Pow((1 + rate/100), time)) CI = amount - principal fmt.Println("\nCompound Interest = ", CI) fmt.Println("Total future amount = ", amount) }

**Output**

Enter the principal amount: 10000 Enter the interest rate: 4 Enter the time period: 2 Compound Interest = 816.0000000000018 Total future amount = 10816.000000000002

**How Does This Program Work?**

var principal, rate, time, CI, amount float64

We have declared five float data type variables named principal, rate, time, CI and amount.

fmt.Print("Enter the principal amount: ") fmt.Scanln(&principal)

The program asks the user to enter the principal amount. The entered value gets stored in the principal variable.

fmt.Print("Enter the interest rate: ") fmt.Scanln(&rate)

The rate of interest entered by the user gets stored in the rate-named variable.

fmt.Print("Enter the time period: ") fmt.Scanln(&time)

Similarly, the time period entered by the user is stored in the time-named variable.

amount = principal * (math.Pow((1 + rate/100), time))

Now, the compound interest amount is calculated using the formula: principal x (1 + rate/100)time, where the time period is given in years.

The Golang language contains an inbuilt package named math which includes the Pow method.

The Pow() method is used to calculate x to the power y.

**math.Pow(x, y) => x**^{y}

The value computed using the above formula is the compound interest amount and gets stored in the amount variable.

CI = amount - principal

To find the compound interest, we subtract the principal amount from the compound interest amount. This will give us compound interest. The compound interest gets stored in the CI variable.

fmt.Println("\nCompound Interest = ", CI) fmt.Println("Total future amount = ", amount)

At last, the compound interest and the total future amount are printed on the screen with the help of fmt.Println() function.

**Conclusion**

In this post, you learned how to write a program to calculate compound interest using the Go Programming language.

If you have any doubt regarding the program, comment down your queries in the comment section.