Objective
Write a C# program that calculate the surface area and volume of a sphere, given its radius (surface area = 4 * pi * radius squared; volume = 4/3 * pi * radius cubed).
Note: For floating-point numbers, you should use Convert.ToSingle(...)
Write Your C# Exercise
C# Exercise Example
using System; // Import the System namespace to use basic classes like Console
class Program // Define the main class of the program
{
static void Main() // The entry point of the program
{
// Ask the user for the radius of the sphere
Console.Write("Enter the radius of the sphere: "); // Display prompt for radius
float radius = Convert.ToSingle(Console.ReadLine()); // Read and convert the user's input for radius to a single precision float
// Calculate the surface area of the sphere: 4 * pi * radius^2
float surfaceArea = 4 * (float)Math.PI * (radius * radius); // Use Math.PI for the value of pi
// Calculate the volume of the sphere: (4/3) * pi * radius^3
float volume = (4f / 3f) * (float)Math.PI * (radius * radius * radius); // Volume formula using Math.PI for pi
// Display the calculated surface area and volume
Console.WriteLine($"Surface Area of the sphere: {surfaceArea:F2} square units"); // Display the surface area, formatted to 2 decimal places
Console.WriteLine($"Volume of the sphere: {volume:F2} cubic units"); // Display the volume, formatted to 2 decimal places
}
}