find the area of a regular polygon inscribed in a circle in go

Here's one way to find the area of a regular polygon inscribed in a circle in Go:

main.go
package main

import (
	"fmt"
	"math"
)

func main() {
	numSides := 6 // change this to the number of sides in the polygon
	radius := 5.0 // change this to the radius of the circle

	// calculate the side length of the polygon
	sideLength := 2 * radius * math.Sin(math.Pi/float64(numSides))

	// calculate the area of the polygon
	area := 0.5 * float64(numSides) * sideLength * radius

	fmt.Printf("The area of a regular %d-gon inscribed in a circle of radius %.2f is %.2f\n", numSides, radius, area)
}
503 chars
20 lines

In this example, we first define numSides as the number of sides in the regular polygon and radius as the radius of the circle. We then use the formula sideLength = 2 * radius * sin(pi/n) to calculate the length of one side of the polygon. Finally, we use the formula area = 0.5 * n * sideLength * radius to find the area of the polygon, where n is the number of sides.

Note that we use math.Sin and math.Pi from Go's built-in math package to perform the necessary calculations.

gistlibby LogSnag