To find the volume of a sphere in C#, we can use the formula:
V = (4/3) * Pi * r^3
where V is the volume, Pi is the mathematical constant Pi, and r is the radius of the sphere.
Here is the C# code to compute the volume of a sphere using this formula:
main.cs255 chars10 lines
In this example, we have set the radius of the sphere to be 5.0, but you can modify this to any value you want. The Math.Pow
method is used to raise the radius to the power of 3, and the Math.PI
constant is used for the value of Pi. The Console.WriteLine
statement is used to display the result on the console.
The output of this program will be:
main.cs40 chars2 lines
This means that the volume of the sphere with radius 5 is approximately 523.6 cubic units.
gistlibby LogSnag