Golang Program to find volume and surface area of a cube


February 27, 2022, Learn eTutorial
1182

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

How to find the volume and surface area of a cube

A cube is a three dimensional solid figure, having 6 square faces. The volume of a cube is the total three-dimensional space occupied by a cube. The volume of the cube can be found by multiplying the edge length three times.

Cube Volume = l * l * l
 

Surface area of cube is the sum of areas of all the faces of cube that covers it. The formula for surface area is given by six times of square of length of the sides of cube.
 

Surface Area of a Cube = 6l²

The lateral surface area of a cube is the area of the four sides.

The Lateral Surface Area of a Cube = 4 * (l * l)

Where l is length of any side of a Cube

 

How to find volume and surface area of a cube in GO Program

Here we are showing how to find the volume and surface area of a cube in the Go language. Here variable len holds the length of the cube. Other variables SA, Vol, LA are used as the result variable of surface area, volume, lateral surface area. Use the mathematical functions for the calculation. Volume is found by using l * l * l and surface area by using 6l². Finally print the results. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt

STEP 2: Start function main()

STEP 3: Declare the variable len,SA, Vol, LA

STEP 4: Read the length of any side of cube len

STEP 5: Calculate the surface area by using 6l²

STEP 6: Calculate the volume by using l * l * l

STEP 7: Calculate the lateral surface area by using 4 * (l * l)

STEP 8: Print the SA, Vol, LA using fmt.Println()

 

Golang Source Code

                                          package main

import "fmt"

func main() {

    var len, SA, Vol, LA float32

    fmt.Print("Enter the length of any side of a cube = ")
    fmt.Scanln(&len;)

    SA = 6 * (len * len)
    Vol = len * len * len
    LA = 4 * (len * len)

    fmt.Println("\nThe volume of a cube            = ", Vol)
    fmt.Println("The surface area of a cube      = ", SA)
    fmt.Println("Lateral surface area of a cube  = ", LA)
}

                                      

OUTPUT

Enter the length of any side of a cube = 7
The volume of a cube            = 343
The surface area of a cube      = 294
Lateral surface area of a cube  = 196