find the volume of a sphere in csharp

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.cs
using System;

class MainClass {
  public static void Main (string[] args) {
    double radius = 5.0;
    double volume = (4.0/3.0) * Math.PI * Math.Pow(radius, 3);
    Console.WriteLine("Volume of sphere with radius {0} = {1:F2}", radius, volume);
  }
}
255 chars
10 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.cs
Volume of sphere with radius 5 = 523.60
40 chars
2 lines

This means that the volume of the sphere with radius 5 is approximately 523.6 cubic units.

gistlibby LogSnag