Ejercicio
Esfera, float
Objetivo
Calcular la superficie y el volumen de una esfera, dado su radio (superficie = 4 * pi * radio al cuadrado; volumen = 4/3 * pi * radio al cubo).
Sugerencia: para números "flotantes", debe usar Convert.ToSingle (...)
Código de Ejemplo
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
}
}