Double - C# Programming Exercise

In this C# programming exercise, you need to create a program that calculates the perimeter, area, and diagonal of a rectangle, given its width and height. This exercise is an excellent opportunity to practice basic mathematical operations and square root calculations in C#.

The program will prompt the user to input the width and height of the rectangle, then it will calculate: The perimeter, obtained by adding twice the width and twice the height. The area, calculated as the product of the width and height. The diagonal, calculated using the Pythagorean theorem: diagonal = Math.Sqrt(width² + height²). Here’s an example of the program’s output:

Example: Enter the rectangle's width: 3 Enter the rectangle's height: 4 Perimeter: 14 Area: 12 Diagonal: 5

This exercise focuses on basic C# programming concepts and demonstrates the use of methods like Math.Sqrt().

 Category

Basic Data Types

 Exercise

Double

 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

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
    }
}

 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#.

  •  Calculate values of a function

    In this C# programming exercise, you need to create a program that calculates and displays certain values of the function y = x² - 2x + 1, using integer numbers for ...

  •  Display a function

    In this C# programming exercise, you must create a program to "draw" the graph of the function y = (x - 4)² for integer values of x ranging from -1 to 8. The result will be visuall...

  •  Float, speed units

    In this C# exercise, you are asked to write a program that prompts the user for two important pieces of information: the distance in meters and the time taken,...

  •  Sphere, float

    In this C# exercise, you are required to write a program that calculates the surface area and volume of a sphere, given its radius. The formulas provided are as follo...

  •  Vowel - switch

    In this C# exercise, you are asked to write a program that prompts the user for a symbol and, using the switch statement, determines whether the symbol entered is a ...

  •  Vowel - if

    In this C# exercise, you are asked to write a program that prompts the user for a symbol and determines if it is a vowel (in lowercase), a digit, or any other ...