Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find Volume and Surface Area of a Sphere using C#?
To find the volume and surface area of a sphere using C#, we need to apply mathematical formulas that use the sphere's radius. A sphere is a perfectly round three-dimensional object where every point on its surface is equidistant from its center.
Formulas
The mathematical formulas for a sphere are −
Surface Area = 4 × ? × r² Volume = (4/3) × ? × r³
Where r is the radius of the sphere and ? (pi) is approximately 3.14159 or can be represented as 22/7.
Using Math.PI for Accurate Calculations
Example
using System;
public class SphereCalculator {
public static void Main(string[] args) {
double radius = 15;
double surfaceArea, volume;
// Calculate surface area using Math.PI
surfaceArea = 4 * Math.PI * radius * radius;
// Calculate volume using Math.PI
volume = (4.0 / 3.0) * Math.PI * radius * radius * radius;
Console.WriteLine("Radius of Sphere: " + radius);
Console.WriteLine("Surface Area of Sphere: " + Math.Round(surfaceArea, 2));
Console.WriteLine("Volume of Sphere: " + Math.Round(volume, 2));
}
}
The output of the above code is −
Radius of Sphere: 15 Surface Area of Sphere: 2827.43 Volume of Sphere: 14137.17
Using Approximation (22/7) for Pi
Example
using System;
public class SphereWithApproximation {
public static void Main(string[] args) {
double radius = 15;
double surfaceArea, volume;
double pi = 22.0 / 7.0;
// Calculate surface area using 22/7 approximation
surfaceArea = 4 * pi * radius * radius;
// Calculate volume using 22/7 approximation
volume = (4.0 / 3.0) * pi * radius * radius * radius;
Console.WriteLine("Using ? ? 22/7:");
Console.WriteLine("Radius: " + radius);
Console.WriteLine("Surface Area: " + Math.Round(surfaceArea, 2));
Console.WriteLine("Volume: " + Math.Round(volume, 2));
}
}
The output of the above code is −
Using ? ? 22/7: Radius: 15 Surface Area: 2828.57 Volume: 14142.86
Interactive Sphere Calculator with Multiple Radii
Example
using System;
public class MultipleSpheres {
public static void CalculateSphere(double radius) {
double surfaceArea = 4 * Math.PI * Math.Pow(radius, 2);
double volume = (4.0 / 3.0) * Math.PI * Math.Pow(radius, 3);
Console.WriteLine($"Radius: {radius}");
Console.WriteLine($"Surface Area: {Math.Round(surfaceArea, 2)}");
Console.WriteLine($"Volume: {Math.Round(volume, 2)}");
Console.WriteLine("---");
}
public static void Main(string[] args) {
double[] radii = {5, 10, 15, 20};
Console.WriteLine("Sphere Calculations:");
Console.WriteLine("===================");
foreach (double r in radii) {
CalculateSphere(r);
}
}
}
The output of the above code is −
Sphere Calculations: =================== Radius: 5 Surface Area: 314.16 Volume: 523.6 --- Radius: 10 Surface Area: 1256.64 Volume: 4188.79 --- Radius: 15 Surface Area: 2827.43 Volume: 14137.17 --- Radius: 20 Surface Area: 5026.55 Volume: 33510.32 ---
Key Points
-
Use
Math.PIfor more accurate calculations instead of 22/7 approximation. -
Use
Math.Pow(radius, 2)orradius * radiusfor squaring the radius. -
Use
Math.Round()to limit decimal places in the output for better readability. -
Surface area is measured in square units (e.g., cm²), while volume is measured in cubic units (e.g., cm³).
Conclusion
Calculating the volume and surface area of a sphere in C# requires applying the mathematical formulas using the radius value. Use Math.PI for precise calculations and Math.Round() to format the output appropriately.
