In this post, we will learn how to write a program to calculate simple interest using the Go programming language.
Simple interest is a method to calculate the amount of interest charged on a principal amount at a given rate for a given period of time.
To calculate the simple interest, we use the following formula:
SI = (P x R X T)/ 100
Where,
- P is the principal amount
- R is the rate of interest
- T is the Time Period (in Years)
This Golang program allows the user to enter the principal amount, rate of interest, and time period. Then, it calculates the simple interest using the formula: SI = (P*R*T)/100.
Go Program to Calculate Simple Interest
package main import ( "fmt" ) func main() { var amount, rate, time, SI float64 fmt.Print("Enter the principal amount: ") fmt.Scanln(&amount) fmt.Print("Enter the interest rate: ") fmt.Scanln(&rate) fmt.Print("Enter the time period (annually): ") fmt.Scanln(&time) SI = (amount * rate * time) / 100 fmt.Print("\nSimple Interest = ", SI) }
Output
Enter the principal amount: 12000
Enter the interest rate: 5
Enter the time period (annually): 2
Simple Interest = 1200
How Does This Program Work?
var amount, rate, time, SI float64
We have declared four float data types named amount, rate, time, and SI.
fmt.Print("Enter the principal amount: ") fmt.Scanln(&amount)
The program displays a message to the user asking him to enter the principal amount. The principal amount entered by the user gets stored in the named variable.
fmt.Print("Enter the interest rate: ") fmt.Scanln(&rate)
Similarly, the interest rate is also taken as input from the user. The interest rate gets stored in the rate-named variable.
fmt.Print("Enter the time period (annually): ") fmt.Scanln(&time)
Now, the program asks the user to enter the time. Here, the time interval is calculated annually, so the user is asked to enter the time period in years. The value entered by the user is stored in the time variable.
SI = (amount * rate * time) / 100
We compute simple interest using the values provided by the user.
The simple interest is computed using the mathematical formula SI = (P x R x T)/100.
The value returned after the calculation is stored in the SI variable.
fmt.Print("\nSimple Interest = ", SI)
At last, the simple interest is printed on the screen using the fmt.Print() method.
Conclusion
In this post, you learned how to write a program to calculate simple interest using the Go programming language.
If you have any doubts regarding the program, please leave your queries in the comment section.