find the surface area of a sphere in go

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

surface area = 4 * pi * r^2

Where r is the radius of the sphere and pi is the mathematical constant approximately equal to 3.14159. We can use the math package in Go to achieve this.

Here is the code to find the surface area of a sphere in Go:

main.go
package main

import (
	"fmt"
	"math"
)

func main() {
	var radius float64 = 5
	surfaceArea := 4 * math.Pi * math.Pow(radius, 2)
	fmt.Printf("The surface area of the sphere with radius %.2f is: %.2f", radius, surfaceArea)
}
224 chars
13 lines

In this example, we have taken the radius of the sphere as 5. We have then used the math.Pow() function to calculate the square of the radius and multiply it with 4 * pi to get the surface area. The Printf() function is used to print out the final result to the console.

Output:

main.go
The surface area of the sphere with radius 5.00 is: 314.16
59 chars
2 lines

related categories

gistlibby LogSnag