Golang Program to find volume and surface area of a cuboid


April 1, 2022, Learn eTutorial
1251

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 cuboid

The surface area of a cuboid is the sum of the areas of all 6 faces.
If we label the length(l), width(w), and height(h) then
 

Surface Area of a Cuboid = 2lw + 2lh + 2wh

Volume of a cuboid is the product of length, width and height

Cuboid Volume = lbh

The lateral surface area of a cuboid is the value of the surface area of a cuboid excluding its top and bottom surfaces. The formula for the lateral surface area is

The Lateral Surface Area of a Cuboid = 2h (l + w)

where l = length, w = width, and h = height

 

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

Here we are showing how to find the volume and surface area of a cuboid in the Go language. Here variable len, Wid, Hght hold the length, width, height of the cuboid. 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 lbh and surface area by using 2lw + 2lh + 2wh. 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, Wid, Hght, SA, Vol, LA

STEP 4: Read the length, width, height of the cuboid len, Wid, Hght

STEP 5: Calculate the surface area by using 2lw + 2lh + 2wh

STEP 6: Calculate the volume by using lbh

STEP 7: Calculate the lateral surface area by using 2h (l + w)

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

 

Golang Source Code

                                          package main
import "fmt"

func main() {

    var len, Wid, Hght, SA, Volu, LA float32
    fmt.Print("Enter the length of a cuboid = ")
    fmt.Scanln(&len;)
    fmt.Print("Enter the width of a cuboid  = ")
    fmt.Scanln(&Wid;)
    fmt.Print("Enter the height of a cuboid = ")
    fmt.Scanln(&Hght;)

    SA = 2 * (len*Wid + len*Hght + Wid*Hght)
    Vol = len * Wid * Hght
    LA = 2 * Hght * (len + Wid)

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

OUTPUT

Enter the length of a cuboid = 8
Enter the width of a cuboid  = 5
Enter the height of a cuboid = 6
The volume of a cuboid         = 240
The surface area of a cuboid     = 236
Lateral surface area of a cuboid = 156