find the surface area of a regular polygon inscribed in a cylinder in go

To find the surface area of a regular polygon inscribed in a cylinder, we need to calculate the surface area of the curved part of the cylinder where the polygon is inscribed, as well as the surface area of the polygon itself.

Here's the code in Go:

main.go
package main

import (
	"fmt"
	"math"
)

func main() {
	// define the number of sides of the regular polygon
	n := 6 // for hexagon

	// define the radius of the cylinder and the radius of the inscribed polygon
	r := 5.0
	rp := r * math.Sin(math.Pi/float64(n))

	// calculate the surface area of the curved part of the cylinder
	cs := 2 * math.Pi * r

	// calculate the surface area of the polygon using trigonometry
	ap := math.Pi / float64(n)
	ss := n * rp * math.Sqrt(math.Pow(r, 2)-math.Pow(rp, 2))
	
	// calculate the total surface area
	sa := cs + ss

	// print the results
	fmt.Printf("Surface area of the polygon inscribed in a cylinder:\n%.2f\n", sa)
}
662 chars
29 lines

In this code, we define the number of sides of the regular polygon (in this case, a hexagon), as well as the radius of the cylinder and the radius of the inscribed polygon. We then use trigonometry to calculate the surface area of the polygon itself, and add it to the surface area of the curved part of the cylinder to get the total surface area. Finally, we print the result.

Note that we use the math package in Go to perform the trigonometric calculations.

gistlibby LogSnag