The volume of a cone is the total amount of space that the cone occupies in a three-dimensional plane. The product of Pi (π), the cone's height, the square of its radius, and 1/3 will give us the volume of a cone.
We can easily calculate a cone's volume in C#.
(1.0 / 3) * Math.PI * r * r * h;
Math.PI
: This is the mathematical constant Pi.r
: This is the radius of the cone.h
: This is the height of the cone.The value returned is a double
value, representing the volume of the specified cone.
using System; class ConeVolume { // Create a function to get the areas static void GetVolume(double h, double r){ double volume = (1.0 / 3) * Math.PI * r * r * h; Console.WriteLine("The volume of the cone with height " + h + " and radius " + r + " is " + volume); } // The main method static void Main() { // Create the radii of some cones double r1 = 10; double r2 = 1.5; double r3 = 4.5; // Create the heights of the cones double h1 = 2.2; double h2 = 5; double h3 = 20.5; // Get the volumes of the cones GetVolume(h1, r1); GetVolume(h2, r2); GetVolume(h3, r3); } }
GetVolume()
. It takes two parameters: height and radius. This method calculates the area of the cone and shows us the result on the console.Main()
function, we create the radii of the cones whose volume we want to get.GetVolume()
method, and pass the heights and radii we created. Then, this method reveals the volume on the console.RELATED TAGS
CONTRIBUTOR
View all Courses