find the volume of a triangular prism in go

To find the volume of a triangular prism in Go programming language, we need to have the base area and the height of the prism. The formula to calculate the volume of a triangular prism is:

main.go
Volume = (Base Area * Height) / 3
34 chars
2 lines

Here is a function in Go that takes in the base length, base width, and the height of the prism as input and returns the volume.

main.go
package main

import "fmt"

func main() {
    baseLength := 10.0
    baseWidth := 5.0
    height := 7.5

    // Calculate the base area of the triangle
    baseArea := 0.5 * baseLength * baseWidth

    // Calculate the volume of the triangular prism
    volume := (baseArea * height) / 3

    fmt.Printf("The volume of the triangular prism is %.2f units^3\n", volume)
}
370 chars
18 lines

In this example, we have considered the base length as 10 units, base width as 5 units, and height as 7.5 units. The program first calculates the base area of the triangle using the formula for the area of a triangle. Then, the volume of the prism is calculated using the formula mentioned above.

The output of the program will be:

main.go
The volume of the triangular prism is 125.00 units^3
53 chars
2 lines

This means that the volume of the prism is 125 cubic units.

gistlibby LogSnag