Go Program to Calculate Simple Interest

 

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.

 

Leave a Reply

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