The capacity of a sphere is known as the volume of that sphere. In other words, it is the space occupied by the sphere. In C#, we can easily find the volume of a sphere.
4.0 / 3 * Math.PI * r * r * r
Math.PI
: This is an inbuilt value for the mathematic PI in C#.
r
: This is the radius of the given sphere.
using System;class HelloWorld{// create function to get the volumestatic void GetVolume(double r){double volume = 4/3 * Math.PI * r * r * r;Console.WriteLine("The volume of the sphere with radius "+r+" is "+volume);}// main methodstatic void Main(){// create radius of some spheresdouble r1 = 10;double r2 = 1.5;double r3 = 4.5;double r4 = 8;// get the volume of the spheresGetVolume(r1);GetVolume(r2);GetVolume(r3);GetVolume(r4);}}
GetVolume()
method. This method takes the radius of the given sphere and calculates its volume. Then, it prints the result to the console screen.GetVolume()
method and display the results on the console screen.