find the volume of a regular polygon inscribed in a cone in csharp

main.cs
using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int numOfSides = 6; // number of sides of the regular polygon
            double radius = 5; // radius of the base of the cone
            double height = 10; // height of the cone

            double circumference = 2 * Math.PI * radius; // circumference of the base of the cone
            double sideLength = circumference / numOfSides; // length of one side of the polygon
            double apothem = radius * Math.Cos(Math.PI / numOfSides); // apothem of the polygon

            double baseArea = (numOfSides * Math.Pow(sideLength, 2)) / (4 * Math.Tan(Math.PI / numOfSides)); // area of the base polygon
            double volume = (baseArea * height) / 3; // volume of the cone

            Console.WriteLine("Num of sides: " + numOfSides);
            Console.WriteLine("Radius: " + radius);
            Console.WriteLine("Height: " + height);
            Console.WriteLine("------------------------");
            Console.WriteLine("Volume of the polygon inscribed in the cone: " + volume);
        }
    }
}
1143 chars
28 lines

In this program, we initialize the number of sides of the regular polygon, radius and height of the cone. Then we calculate the circumference of the base of the cone, the length of one side of the polygon and the apothem of the polygon. Using these values, we calculate the area of the base polygon and the volume of the cone. Finally, we print the volume of the polygon inscribed in the cone.

gistlibby LogSnag