Objectivo
Calcula el perímetro, el área y la diagonal de un rectángulo, dado su ancho y su altura.
(Sugerencia: use y = Math.Sqrt(x) para calcular una raíz cuadrada)
Ejemplo Ejercicio C#
Mostrar Código C#
using System; // Import the System namespace to use basic classes like Console and Math
class Program // Define the main class of the program
{
static void Main() // The entry point of the program
{
double width, height, perimeter, area, diagonal; // Declare variables for width, height, perimeter, area, and diagonal
// Ask the user for the width of the rectangle
Console.Write("Enter the width of the rectangle: ");
width = Convert.ToDouble(Console.ReadLine()); // Read and convert the input to double
// Ask the user for the height of the rectangle
Console.Write("Enter the height of the rectangle: ");
height = Convert.ToDouble(Console.ReadLine()); // Read and convert the input to double
// Calculate the perimeter of the rectangle
perimeter = 2 * (width + height); // Perimeter formula: P = 2 * (width + height)
// Calculate the area of the rectangle
area = width * height; // Area formula: A = width * height
// Calculate the diagonal of the rectangle using the Pythagorean theorem
diagonal = Math.Sqrt(Math.Pow(width, 2) + Math.Pow(height, 2)); // Diagonal formula: d = sqrt(width^2 + height^2)
// Display the results
Console.WriteLine($"Perimeter: {perimeter}"); // Display the perimeter
Console.WriteLine($"Area: {area}"); // Display the area
Console.WriteLine($"Diagonal: {diagonal}"); // Display the diagonal
}
}
Salida
Case 1:
Enter the width of the rectangle: 10
Enter the height of the rectangle: 5
Perimeter: 30
Area: 50
Diagonal: 11.180339887498949
Case 2:
Enter the width of the rectangle: 7
Enter the height of the rectangle: 3
Perimeter: 20
Area: 21
Diagonal: 7.615777322923054
Case 3:
Enter the width of the rectangle: 12
Enter the height of the rectangle: 9
Perimeter: 42
Area: 108
Diagonal: 15.0
Código de Ejemplo Copiado!
Comparte este Ejercicio C# Sharp
¡Explora nuestro conjunto de ejercicios de programación C# Sharp! Estos ejercicios, diseñados específicamente para principiantes, te ayudarán a desarrollar una sólida comprensión de los conceptos básicos de C#. Desde variables y tipos de datos hasta estructuras de control y funciones simples, cada ejercicio está diseñado para desafiarte de manera gradual a medida que adquieres confianza en la codificación en C#.
En este ejercicio de programación en C#, se debe crear un programa que calcule y muestre ciertos valores de la función y = x² - 2x + 1, utilizando números enteros par...
En este ejercicio de programación en C#, se debe crear un programa que "dibuje" la gráfica de la función y = (x - 4)² para valores enteros de x que van de -1 a 8. El resultado será...
En este ejercicio en C#, se te pide que escribas un programa que solicite al usuario dos datos importantes: la distancia en metros y el tiempo transcurrido en ...
En este ejercicio en C#, deberás escribir un programa que calcule el área de superficie y el volumen de una esfera dado su radio. Para ello, se te proporcionan las si...
En este ejercicio en C#, se te pide escribir un programa que solicite al usuario un símbolo y, utilizando la estructura de control switch, determine si el símbolo ing...
En este ejercicio en C#, se te pide escribir un programa que solicite al usuario un símbolo y determine si es una vocal (en minúsculas), un número o cualquier ...