- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How to Calculate Simple Interest using Golang code?

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

## Formula

simple_Interest = ( p * r * t) / 100

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 simple interest is.

Simple_interest = (1000 * 4 * 2) / 100 = 80

## Finding the simple interest within the function

### Algorithm

**STEP 1**− Declaring the variables for the principal amount, rate of interest, time, and the simple 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 simple interest using the above formula within the function.**STEP 4**− Printing the result.

### Example 1

In this example, we will find the simple interest within the function

#### Time Complexity

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

#### Space Complexity

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

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

### Output

Program to find simple interest. principal = 3000 Rate of Interest = 4 Time = 3 The simple interest is 360 (Finding the simple interest within the function)

#### Description of code:

**var principal, rateOfInterest, time, simpleInterest float64**− In this line we are declaring the principal, rate of interest, time, and simple interest that we are going to use later. As the interest can be in decimal so we have used the float data type.**simpleInterest = (principal * rateOfInterest * time) / 100**− In this line of code, we are applying the formula and finding the simple interest.**fmt.Println("The simple interest is", simpleInterest)**− In last printing the result.

## Finding the simple interest in different function

### Algorithm

**STEP 1**− Declaring the variables for the principal amount, rate of interest, time, and the simple 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 simple interest the function is returning.**STEP 4**− Printing the result.

### Example 2

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

package main // fmt package provides the function to print anything import "fmt" func simpleInterestFunction(principal, rateOfInterest, time float64) float64 { // finding the simple interest simpleInterest := (principal * rateOfInterest * time) / 100 // returning the simple interest return simpleInterest } func main() { // declaring the floating variables using the var keyword // for storing the principal, rate of interest, and time var principal, rateOfInterest, time, simpleInterest float64 fmt.Println("Program to find simple 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 simple interest function by passing the respective parameter // and storing the result simpleInterest = simpleInterestFunction(principal, rateOfInterest, time) // printing the result fmt.Println("The simple interest is", simpleInterest) fmt.Println("(Finding the simple interest in different function)") }

### Output

Program to find simple interest. Please enter the value of the principal amount = 10000 Please enter the value of the rate of interest = 3 Please enter the value of the time = 4 The simple interest is 1200 (Finding the simple interest in different function)

### Description of code

**var principal, rateOfInterest, time, simpleInterest float64**− In this line we are declaring the principal, rate of interest, time, and simple interest that we are going to use later. As the interest can be in decimal so we have used the float data type.**simpleInterest = (principal * rateOfInterest * time) / 100**− In this line of code, we are applying the formula and finding the simple interest.**fmt.Println("The simple interest is", simpleInterest)**− In last printing the result.**fmt.Scanln(&time)**&minus Taking the input for the time from the user**simpleInterest = simpleInterestFunction(principal, rateOfInterest, time)**− In this line of code we are calling the function that is finding the simple interest by passing the respective parameters.

**fmt.Println("The simple interest is", simpleInterest)**− In last printing the result.

## Conclusion

These are the two ways to find simple 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.