Introduction
Simple interest is a method of calculating the interest charged or earned on a principal amount over a specified period of time at a fixed rate. It is commonly used for loans, savings accounts, or investment purposes. The formula for calculating simple interest is:
Simple Interest (SI) = (Principal × Rate × Time) / 100
In this program, we will calculate the simple interest based on the user-provided values for the principal amount, rate of interest, and time duration. The program will output the calculated simple interest.
Objective
The objective of this program is to implement a simple interest calculator using the Go programming language. It will allow the user to input the principal amount, interest rate, and time period, and then calculate the simple interest based on the formula provided.
Go Program Code
package main
import (
"fmt"
)
func main() {
// Declare variables to store the user inputs
var principal, rate, time, interest float64
// Input: Get the principal, rate, and time from the user
fmt.Print("Enter the principal amount: ")
fmt.Scanf("%f", &principal)
fmt.Print("Enter the rate of interest: ")
fmt.Scanf("%f", &rate)
fmt.Print("Enter the time period in years: ")
fmt.Scanf("%f", &time)
// Calculate simple interest using the formula
interest = (principal * rate * time) / 100
// Output: Display the calculated simple interest
fmt.Printf("The Simple Interest is: %.2f\n", interest)
}
Explanation of the Program Structure
The program is simple and follows a step-by-step approach:
- Imports: The program uses the
fmt
package to handle input and output operations. - Variable Declaration: The program declares variables
principal
,rate
,time
, andinterest
to store the user input and the calculated interest value. - Input Section: The
fmt.Scanf
function is used to capture user inputs for principal, rate, and time. - Interest Calculation: The simple interest formula
interest = (principal * rate * time) / 100
is applied to compute the interest. - Output: The result is displayed using
fmt.Printf
with two decimal precision, showing the calculated simple interest.
How to Run the Program
To run this Go program, follow these steps:
- Install Go on your machine from the official Go website: https://golang.org/dl/
- Open a text editor or an IDE (such as VSCode or GoLand) and create a new file, for example
simple_interest.go
. - Copy the program code provided above and paste it into the file.
- Open the terminal or command prompt and navigate to the directory where your
simple_interest.go
file is saved. - Run the program using the following command:
go run simple_interest.go
- Input the values when prompted: principal amount, rate of interest, and time period in years.
- The program will calculate and display the simple interest.