Simple Interest Calculator in Go

This program calculates the simple interest based on the principal amount, the rate of interest, and the time period. Simple interest is calculated using the formula:

Simple Interest (SI) = (Principal * Rate * Time) / 100

Go Program

package main

import (
    "fmt"
)

// CalculateSimpleInterest calculates simple interest
// Principal: The principal amount
// Rate: The rate of interest per year
// Time: The time period in years
// Returns the calculated simple interest
func CalculateSimpleInterest(principal float64, rate float64, time float64) float64 {
    // Calculate simple interest using the formula (Principal * Rate * Time) / 100
    return (principal * rate * time) / 100
}

func main() {
    // Define principal amount, rate of interest, and time period
    var principal float64 = 1000    // Principal amount
    var rate float64 = 5           // Rate of interest per year
    var time float64 = 2           // Time period in years

    // Calculate simple interest
    simpleInterest := CalculateSimpleInterest(principal, rate, time)

    // Print the calculated simple interest
    fmt.Printf("Simple Interest = %.2f\n", simpleInterest)
}
    

Program Explanation

The program is structured into two main parts:

  1. Function Definition:
    • CalculateSimpleInterest(principal float64, rate float64, time float64) float64: This function takes three arguments: principal, rate, and time, all of type float64. It calculates the simple interest using the formula (Principal * Rate * Time) / 100 and returns the result as a float64.
  2. Main Function:
    • main(): This is the entry point of the program. It defines the variables for principal, rate, and time, and calls the CalculateSimpleInterest function to compute the simple interest. Finally, it prints the calculated simple interest using fmt.Printf.

Code Documentation

The code includes comments to explain the purpose and functionality of different sections. The CalculateSimpleInterest function is documented with a comment that describes its parameters and return value.

 

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 :)