Golang
Golang

 

 

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, and interest 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:

  1. Install Go on your machine from the official Go website: https://golang.org/dl/
  2. Open a text editor or an IDE (such as VSCode or GoLand) and create a new file, for example simple_interest.go.
  3. Copy the program code provided above and paste it into the file.
  4. Open the terminal or command prompt and navigate to the directory where your simple_interest.go file is saved.
  5. Run the program using the following command:
    go run simple_interest.go
  6. Input the values when prompted: principal amount, rate of interest, and time period in years.
  7. The program will calculate and display the simple interest.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)