Objective
Write a C# program that calculate the perimeter, area, and diagonal of a rectangle, given its width and height.
(Hint: use y = Math.Sqrt(x) to calculate a square root)
Write Your C# Exercise
C# Exercise Example
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
}
}