Go Program to Calculate Area and Perimeter of Different Shapes
This page contains a Go program that calculates the area and perimeter of different shapes: circle, rectangle, and triangle.
Program Explanation
The Go program follows these steps:
- Define the main package and import necessary packages: Use
package main
and import thefmt
andmath
packages. - Define functions for each shape: Create separate functions to calculate the area and perimeter of a circle, rectangle, and triangle.
- Main Function: The
main
function is the entry point of the program where the functions are called and results are printed.
Go Program Code
package main
import (
"fmt"
"math"
)
/**
* Function to calculate the area and perimeter of a circle.
* @param radius the radius of the circle
* @return the area and perimeter of the circle
*/
func circleAreaPerimeter(radius float64) (float64, float64) {
area := math.Pi * radius * radius
perimeter := 2 * math.Pi * radius
return area, perimeter
}
/**
* Function to calculate the area and perimeter of a rectangle.
* @param length the length of the rectangle
* @param width the width of the rectangle
* @return the area and perimeter of the rectangle
*/
func rectangleAreaPerimeter(length, width float64) (float64, float64) {
area := length * width
perimeter := 2 * (length + width)
return area, perimeter
}
/**
* Function to calculate the area and perimeter of a triangle.
* @param a the length of the first side
* @param b the length of the second side
* @param c the length of the third side
* @return the area and perimeter of the triangle
*/
func triangleAreaPerimeter(a, b, c float64) (float64, float64) {
perimeter := a + b + c
s := perimeter / 2 // semi-perimeter
area := math.Sqrt(s * (s - a) * (s - b) * (s - c)) // Heron's formula
return area, perimeter
}
/**
* Main function which is the entry point of the program.
*/
func main() {
// Calculate and print area and perimeter for a circle
radius := 5.0
circleArea, circlePerimeter := circleAreaPerimeter(radius)
fmt.Printf("Circle: Radius = %.2f, Area = %.2f, Perimeter = %.2f\n", radius, circleArea, circlePerimeter)
// Calculate and print area and perimeter for a rectangle
length := 10.0
width := 5.0
rectangleArea, rectanglePerimeter := rectangleAreaPerimeter(length, width)
fmt.Printf("Rectangle: Length = %.2f, Width = %.2f, Area = %.2f, Perimeter = %.2f\n", length, width, rectangleArea, rectanglePerimeter)
// Calculate and print area and perimeter for a triangle
a, b, c := 3.0, 4.0, 5.0
triangleArea, trianglePerimeter := triangleAreaPerimeter(a, b, c)
fmt.Printf("Triangle: Sides = %.2f, %.2f, %.2f, Area = %.2f, Perimeter = %.2f\n", a, b, c, triangleArea, trianglePerimeter)
}
Program Details
The program contains the following:
circleAreaPerimeter
Function: Calculates and returns the area and perimeter of a circle given its radius.rectangleAreaPerimeter
Function: Calculates and returns the area and perimeter of a rectangle given its length and width.triangleAreaPerimeter
Function: Calculates and returns the area and perimeter of a triangle given the lengths of its sides using Heron’s formula.main
Function: The entry point of the program where the functions are called with sample values and the results are printed.
Running the Program
To run the program:
- Save the code in a file named
main.go
. - Open a terminal and navigate to the directory where the file is saved.
- Run the program using the command:
go run main.go
.
Explanation of the Go Program:
- Package and Imports: The program starts with the
main
package declaration and imports the necessaryfmt
andmath
packages. - Functions for Each Shape:
circleAreaPerimeter
: Calculates the area and perimeter of a circle given its radius.rectangleAreaPerimeter
: Calculates the area and perimeter of a rectangle given its length and width.triangleAreaPerimeter
: Calculates the area and perimeter of a triangle given the lengths of its sides using Heron’s formula.
- main Function:
- Defines sample values for a circle, rectangle, and triangle.
- Calls the respective functions to calculate the area and perimeter.
- Prints the results to the console using
fmt.Printf
.