Go Program To Calculate Compound Interest

In this post, you will learn how to calculate compound interest using Go Programming language.

Hello coders, in this post we will learn how to calculate compound interest using Go Programming language.

Go Program To Calculate Compound Interest

Compound Interest is the interest calculated based on both the initial principal amount and accumulated interest from previous periods.

Compound Interest is calculated using this formula:

Compound Interest = Principal Amount * (1 + Interest Rate)^Time

We will be using the same formula to calculate the Compound Interest in the program.

So, without any delay, let's begin the tutorial.

Table Of Contents 👊

Go Program To Calculate Compound Interest

package main
import(
  "fmt"
  "math"
  ) 

func main(){
  var principal, rate, time, future_interest, compound float64
  
  // Asking for Input 
  fmt.Print("Enter Principal Amount: ")
  fmt.Scan(&principal)
  
  fmt.Print("Enter Interest Rate: ")
  fmt.Scan(&rate)
  
  fmt.Print("Enter Time Period: ")
  fmt.Scan(&time)
  
  future_interest = principal * (math.Pow((1 + rate/100), time))
  compound = future_interest - principal
  
  fmt.Println("The Compound Interest is", compound)
  fmt.Println("The future Compound Interest is", future_interest)
}

Output

Enter Principal Amount: 5000
Enter Interest Rate: 7
Enter Time Period: 3
The Compound Interest is 1125.2150000000001
The future Compound Interest is 6125.215

How Does This Program Work ?

  var principal, rate, time, future_interest, compound float64

In this program, we have declared five float data type variables principal, rate, time, future_interest and compound.

  // Asking for Input 
  fmt.Print("Enter Principal Amount: ")
  fmt.Scan(&principal)
  
  fmt.Print("Enter Interest Rate: ")
  fmt.Scan(&rate)
  
  fmt.Print("Enter Time Period: ")
  fmt.Scan(&time)

Then, the user is asked to enter the values of principal amount, interest rate and time period respectively.

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

Compound Interest is calculated using the formula P(1 + r)^(t) where, P is the initial Principal balance, r is the interest rate and and t is the number of time periods.

  fmt.Println("The Compound Interest is", compound)
  fmt.Println("The future Compound Interest is", future_interest)

Finally, the output is displayed to the screen using fmt.Println() function.

Some of the used terms in this program are as follows:- 

package main - packages are a collection of Go source files that reside in the same directory. The line package main specifies that this file belongs to the main package.

Every Executable Go Application must contain the main function.

math - Package math provides basic constant functions and mathematical functions.

fmt.Println() - The fmt.Println() function in Go language is used to display output.

fmt.Scan() - The fmt.PrintScan() function is used to take input from the user in Go Programming language.

Pow() - Pow returns x**y, the base-x exponential of y.

// - Used for Commenting in Go  

Conclusion

I hope after reading this post, you clearly understood how to calculate compound interest using Go Programming language.

If you still have any doubts regarding the topic, let us know in the comment section.

Sloth Coders is a Learning Platform for Coders, Programmers and Developers to learn from the basics to advance of Coding of different langauges(python, Java, Javascript and many more).

Post a Comment