The area of a sphere is the total area covered by the sphere's outer surface. In C#, we can calculate it using the sphere's radius.
4 * Math.PI * r * r
Math.PI
: This is an built-in value that represents the mathematical value Pi.r
: This represents the radius of the sphere.using System;class HelloWorld{// create function to get the areasstatic void GetArea(double r){double area = 4 * Math.PI * r * r;Console.WriteLine("The area of the sphere with radius "+r+" is "+area);}// main methodstatic void Main(){// create radius of some spheresdouble r1 = 10;double r2 = 1.5;double r3 = 4.5;double r4 = 8;// get the area of the spheresGetArea(r1);GetArea(r2);GetArea(r3);GetArea(r4);}}
GetArea()
. It takes the sphere's radius, calculates the area, then prints the result to the console screen.Main
method, we create the radii of some spheres.GetArea()
method, and display the results on the console.