Calculating Perimeter, Area, and Diagonal of a Rectangle in C#

In this exercise, we will create a C# program to calculate the perimeter, area, and diagonal of a rectangle based on its width and height. The program will use the following formulas:

- Perimeter = 2 * (width + height)
- Area = width * height
- Diagonal = √(width² + height²) (using the Pythagorean theorem)

The program will repeatedly prompt the user to enter the width and height of the rectangle and calculate these values until the user enters `0` for the width. This exercise helps in understanding loops, user input handling, and mathematical operations in C#.



Group

C# Basic Data Types Overview

Objective

1. Continuously ask the user to input the width and height of the rectangle.
2. For each set of inputs, calculate the perimeter, area, and diagonal using the given formulas.
3. Display the results for each rectangle.
4. If the width entered by the user is `0`, exit the loop and stop the program.
5. Ensure proper formatting for the output, especially for the diagonal (showing up to two decimal places).

Write a C# program to calculate the perimeter, area, and diagonal of a rectangle from its width and height (perimeter = sum of the four sides, area = base x height, diagonal using the Pythagorean theorem). It must repeat until the user enters 0 for the width.

Example C# Exercise

 Copy C# Code
using System;

class Program
{
    static void Main()
    {
        double width, height;

        // Continuously ask for input until width is 0
        while (true)
        {
            // Ask for the width and height of the rectangle
            Console.Write("Enter the width of the rectangle (enter 0 to exit): ");
            width = Convert.ToDouble(Console.ReadLine());

            // Exit if width is 0
            if (width == 0)
            {
                break;
            }

            Console.Write("Enter the height of the rectangle: ");
            height = Convert.ToDouble(Console.ReadLine());

            // Calculate the perimeter, area, and diagonal
            double perimeter = 2 * (width + height);
            double area = width * height;
            double diagonal = Math.Sqrt(Math.Pow(width, 2) + Math.Pow(height, 2));

            // Display the results
            Console.WriteLine($"Perimeter: {perimeter}");
            Console.WriteLine($"Area: {area}");
            Console.WriteLine($"Diagonal: {diagonal:F2}\n");  // Format diagonal to 2 decimal places
        }

        Console.WriteLine("Program ended.");
    }
}

 Output

Enter the width of the rectangle (enter 0 to exit): 5
Enter the height of the rectangle: 10
Perimeter: 30
Area: 50
Diagonal: 11.18

Enter the width of the rectangle (enter 0 to exit): 3
Enter the height of the rectangle: 4
Perimeter: 14
Area: 12
Diagonal: 5.00

Enter the width of the rectangle (enter 0 to exit): 0
Program ended.

Share this C# Exercise

More C# Practice Exercises of C# Basic Data Types Overview

Explore our set of C# Practice Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.