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

To find the area of a regular polygon inscribed in a circle, we need to know the number of sides (n) of the polygon and the radius (r) of the circle. The formula for the area (A) of the polygon is:

A = 0.5 * n * r^2 * sin(2π/n)

Here's the code in C#:

main.cs
using System;

namespace PolygonArea
{
    class Program
    {
        static void Main(string[] args)
        {
            // Inputs
            int n = 6;     // Number of sides
            double r = 5;  // Radius of circle

            // Calculate area
            double A = 0.5 * n * Math.Pow(r, 2) * Math.Sin(2 * Math.PI / n);

            // Output result
            Console.WriteLine("Area of the polygon: " + A);
        }
    }
}
444 chars
21 lines

In this example, we have a regular hexagon with a radius of 5. The output will be:

main.cs
Area of the polygon: 64.9519052838329
38 chars
2 lines

related categories

gistlibby LogSnag