How to Calculate Compound Interest using Golang code?


In this tutorial, we are going to see the program for calculating compound interest in Golang. It is a method of finding the accurate interest on loans in the banking and finance sector using factors like principal amount, rate per annum, and time. Due to more accuracy, the rates are higher than the simple interest.

Formula

compound_Interest = P * ( 1 + R / 100) ^ T
P = Principal amount
R = Rate per annum
T = Time

For example, suppose the principal amount is 1000, the rate of interest is 4 and the interval is 2 years then the compound interest is.

compound_interest = 1000 * (1 + 4 / 100 ) ^2
= 1081.6000000000001

Finding the compound interest within the function

Algorithm

  • STEP 1 − Declaring the variables for the principal amount, rate of interest, time, and the compound interest of float64 data type.

  • STEP 2 − Taking the input for the principal amount, rate of interest, and time from the user.

  • STEP 3 − Finding the compound interest using the above formula within the function.

  • STEP 4 − Printing the result.

Time Complexity

O(1) - The time complexity is constant because no matter what is the input the program will take sane time.

Space Complexity

O(1) - The variables are static in the program so the space complexity is also constant

Example 1

In this example, we will find the compound interest within the function.

package main // fmt package provides the function to print anything import ( "fmt" "math" ) func main() { // declaring the floating variables using the var keyword // for storing the principal, rate of interest, and time var principal, rateOfInterest, time, compoundInterest float64 fmt.Println("Program to find compound interest.") // initializing the principal principal = 3000 // initializing the rate rateOfInterest = 4 // initializing the time = 3 // finding the compound interest compoundInterest = (principal * math.Pow(1+rateOfInterest/100, time)) // printing the result fmt.Println("Principal =", principal, "\nRate of Interest", rateOfInterest, "\nTime", time, "\nThe compound interest=", compoundInterest) fmt.Println("(Finding the compound interest within the function)") }

Output

Program to find compound interest.
Principal = 3000
Rate of Interest 4
Time 3
The compound interest= 3374.592
(Finding the compound interest within the function) 

Description of code

  • var principal, rateOfInterest, time, compoundInterest float64 − In this line we are declaring the principal, rate of interest, time, and compound interest that we are going to use later. As the interest can be in decimal so we have used the float data type.

  • fmt.Scanln(&principal) − Taking the input for principal from the user.

  • fmt.Scanln(&rateOfInterest) − Taking the input for the rate of interest from the user

  • fmt.Scanln(&time) − Taking the input for the time from the user.

  • compoundInterest = (principal * math.Pow(1+rateOfInterest/100, time)) − In this line of code, we are applying the formula and finding the compound interest. Also, to find the 1+rateOfInterest/100 power of the time we have used the math library in Golang which has the Pow() function which has two parameters of float type.

  • fmt.Println("The compound interest is", compoundInterest) − In later printing the result.

Finding the compound interest in different function

Algorithm

  • STEP 1 − Declaring the variables for the principal amount, rate of interest, time, and the compound interest of float64 data type.

  • STEP 2 − Taking the input for the principal amount, rate of interest, and time from the user.

  • STEP 3 − Calling the function with the principal amount, rate of interest, and time as a parameter, and storing the compound interest the function is returning.

  • STEP 4 − Printing the result.

Example 2

In this example, we will find the compound interest in the separate function and call in the function where we want to print.

package main // fmt package provides the function to print anything import ( "fmt" "math" ) func compoundInterestFunction(principal, rateOfInterest, time float64) float64 { // finding the compound interest compoundInterest := (principal * math.Pow(1+rateOfInterest/100, time)) // returning the compound interest return compoundInterest } func main() { // declaring the floating variables using the var keyword // for storing the principal, rate of interest, and time var principal, rateOfInterest, time, compoundInterest float64 fmt.Println("Program to find compound interest.") // taking the principal as input from the user fmt.Print("Please enter the value of the principal amount = ") fmt.Scanln(&principal) // taking the rate of interest as input from the user fmt.Print("Please enter the value of the rate of interest = ") fmt.Scanln(&rateOfInterest) // taking the value of the time as input from the user fmt.Print("Please enter the value of the time = ") fmt.Scanln(&time) // calling the compound interest function by passing the respective parameter // and storing the result compoundInterest = compoundInterestFunction(principal, rateOfInterest, time) // printing the result fmt.Println("The compound interest is", compoundInterest) fmt.Println("(Finding the compound interest in different function)") }

Output

Program to find compound interest.
Please enter the value of the principal amount = 1000
Please enter the value of the rate of interest = 5
Please enter the value of the time = 3
The compound interest is 1157.6250000000002
(Finding the compound interest in different function)

Description of code:

  • var principal, rateOfInterest, time, compoundInterest float64 − In this line we are declaring the principal, rate of interest, time, and compound interest that we will use later. As the interest can be in decimal so we have used the float data type.

  • fmt.Scanln(&principal) − Taking the input for principal from the user.

  • fmt.Scanln(&rateOfInterest) − Taking the input for the rate of interest from the user.

  • fmt.Scanln(&time) − Taking the input for the time from the user.

  • compoundInterest = compoundInterestFunction(principal, rateOfInterest, time)

    − In this line of code we are calling the function that is finding the compound interest by passing the respective parameters. Also, to find the 1+rateOfInterest/100 power of the time we have used the math library in Golang which has the Pow() function which has two parameters of float type.

  • fmt.Println("The compound interest is", compoundInterest) − In later printing the result.

Conclusion

These are the two ways to find compound interest in Golang. The second way is much better in terms of modularity and code reusability as we can call that function anywhere in the project. To learn more about go you can explore these tutorials.

Updated on: 29-Aug-2022

307 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements