The surface area of a cone is the summation of the curved area of the cone and the area of the base.
Where,
r
is the radius of the cone.h
is the height of the cone.π
is the mathematical pi.In C#, we can easily calculate the area of a cone. All we need to do is apply the formula or logic below.
Math.PI * r * (r + Math.Sqrt(h*h + r*r));
Math.PI
: This is the pi we know in mathematics.r
: This represents the radius of the cone.h
: This represents the height of the cone.The value returned is a double value which represents the surface area of a cone.
using System;class HelloWorld{// create function to get the areasstatic void GetArea(double h, double r){double area = Math.PI * r * (r + Math.Sqrt(h*h + r*r));Console.WriteLine("The area of the cone with height "+h+" and radius "+r+" is "+area);}// main methodstatic void Main(){// create some radii of some conesdouble r1 = 10;double r2 = 1.5;double r3 = 4.5;// create also the heights of each conedouble h1 = 2.2;double h2 = 5;double h3 = 20.5;// get the area of the conesGetArea(h1, r1);GetArea(h2, r2);GetArea(h3, r3);}}
GetArea()
that takes two parameters, height and radius. It calculates the area of the cone and tells the user the result in the console.GetArea()
method and passed it to the radii and heights we have created. We print the results to the console.