Perimeter Area - C# Programming Exercise

In this C# exercise, you are asked to write a program that calculates the perimeter, area, and diagonal of a rectangle from its width and height. The program should use the following mathematical formulas:

Perimeter = sum of the four sides (2 * width + 2 * height)
Area = base x height
Diagonal = using the Pythagorean theorem (sqrt(width^2 + height^2))

The program should continue asking the user for the width and height values until the user enters 0 for the width, at which point it will stop. This exercise is useful for practicing the use of mathematical calculations, conditionals, and loops in C#. Additionally, it will help you get familiar with how to implement logic to repeat processes until a specific condition is met (in this case, when the user enters 0).

Through this exercise, you will also better understand how to use the Pythagorean theorem in programming to calculate distances and diagonals, which is useful in many graphical and simulation applications.

 Category

Basic Data Types

 Exercise

Perimeter Area

 Objective

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.

 Write Your C# Exercise

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
    {
        // Infinite loop to keep asking the user for input until they enter 0 for the width
        while (true)  
        {
            // Ask the user for the width of the rectangle
            Console.Write("Enter the width of the rectangle (0 to quit): ");
            double width = double.Parse(Console.ReadLine());  // Read the user's input and convert it to a double

            // If the width is 0, exit the loop and end the program
            if (width == 0)
            {
                Console.WriteLine("Goodbye!");
                break;  // Exit the while loop and end the program
            }

            // Ask the user for the height of the rectangle
            Console.Write("Enter the height of the rectangle: ");
            double height = double.Parse(Console.ReadLine());  // Read the user's input and convert it to a double

            // Calculate the perimeter of the rectangle (sum of all sides)
            double perimeter = 2 * (width + height);  
            
            // Calculate the area of the rectangle (base x height)
            double area = width * height;  

            // Calculate the diagonal using the Pythagorean theorem (sqrt(width^2 + height^2))
            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}\n");
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Basic Data Types

Explore our set of C# programming 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#.

  •  Hexadecimal and binary

    In this C# exercise, you are asked to write a program that prompts the user for a number and displays it in both hexadecimal and binary. The program should continue r...

  •  Binary

    In this C# exercise, you are asked to write a program that prompts the user for a decimal number and displays its equivalent in binary. The program should continue re...

  •  Conditional and boolean

    In this C# exercise, you are asked to write a program that uses the conditional operator (also known as the ternary operator) to assign a boolean variable named "bothEven...

  •  Exceptions V2

    In this C# exercise, you are asked to write a program that prompts the user for a real number and displays its square root. Additionally, the program must handle any ...

  •  Char

    This exercise in C# aims to develop a program that asks the user for three letters and displays them in reverse order. The user will input one letter at...

  •  Triangle

    This exercise in C# aims to develop a program that prompts the user for a symbol and a width, and then displays a decreasing triangle of the spec...