find the surface area of a rectangular prism in go

To find the surface area of a rectangular prism in Go, we need to use the formula:

Surface Area = 2lw + 2lh + 2wh

Where l, w, and h are the length, width, and height of the rectangular prism.

Here's a code snippet in Go that calculates the surface area of a rectangular prism by taking user input for its dimensions:

main.go
package main

import "fmt"

func main() {
    var l, w, h, surfaceArea float64

    fmt.Println("Enter the length, width and height of the rectangular prism:")
    fmt.Scanln(&l, &w, &h)

    surfaceArea = 2*l*w + 2*l*h + 2*w*h
    fmt.Printf("The surface area of the rectangular prism is: %.2f", surfaceArea)
}
312 chars
14 lines

In this code, we first declare four variables - l, w, h, and surfaceArea. We then use the fmt.Scanln function to take user input for the length, width, and height.

We calculate the surface area using the formula mentioned earlier, and assign the result to the surfaceArea variable.

Finally, we use the fmt.Printf function to print the result with two decimal places.

gistlibby LogSnag