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

To find the surface area of a regular polygon inscribed in a cylinder, we need to calculate both the lateral and base areas of the cylinder, as well as the area of the polygon.

Here is a C# code that takes the number of sides of the regular polygon, the radius and height of the cylinder as input, and returns the surface area of the polygon inscribed in the cylinder:

main.cs
public static double SurfaceAreaOfInscribedRegularPolygon(double numSides, double radius, double height)
{
    double apothem = radius * Math.Cos(Math.PI / numSides);
    double sideLength = 2 * radius * Math.Tan(Math.PI / numSides);

    double polygonArea = 0.5 * numSides * sideLength * apothem;

    double lateralArea = 2 * Math.PI * radius * height;

    double baseArea = numSides * Math.Pow(radius, 2) * Math.Tan(Math.PI / numSides);

    return lateralArea + 2 * polygonArea + 2 * baseArea;
}
502 chars
14 lines

To use this function, you can call it with appropriate parameters:

main.cs
double numSides = 6;  // hexagon
double radius = 2;
double height = 5;

double surfaceArea = SurfaceAreaOfInscribedRegularPolygon(numSides, radius, height);

Console.WriteLine("Surface area of inscribed regular polygon: {0}", surfaceArea);
240 chars
8 lines

gistlibby LogSnag