C# program to calculate the area, perimeter and diagonal of a square has been given below. If length of the side of a square is $s~ unit$, the area, perimeter and diagonal of that square would be $s^2~\text{unit}^2$, $4s~\text{unit}$ and $s\sqrt{2}~\text{unit}$ respectively.

For example, if length of the side of a square is $5~cm$, the area would be $5^2 = 25~\text{cm}^2$ while the perimeter would be $4 \cdot 5 = 20~\text{cm}$. The diagonal will be calculated as $5\cdot \sqrt{2} = 7.071 ~\text{cm}$

In the following section, the algorithm, pseudocode and time-complexity of the program have also been covered.

## 1. Algorithm to calculate the area, perimeter and diagonal of a square

1. Take the length of side $s$ as input.

2. Declare $s^2$ as the area of the square

3. Declare $4s$ as the perimeter of the square

4. Declare $s\sqrt{2}$ as the diagonal of the square

## 2. Pseudocode to calculate the area, perimeter and diagonal of a square

Input : Length of side $s$

Output : Area $A$, Perimeter $P$ and Diagonal $D$ of the square

1. Procedure areaPerimeterDiagonal($s$):

2. $A = s^2$

3. $P = 4s$

4. $D = s\sqrt{2}$

5. Return $A, P, D$

6. End Procedure

## 3. Time complexity to calculate the area, perimeter and diagonal of a square

Time Complexity: O(1)

## 4. C# Program to calculate the area, perimeter and diagonal of a square

Code has been copied
/**************************************
alphabetacoder.com
C# program to calculate the area
,perimeter and diagonal of a square
***************************************/

using System;

namespace AreaPerimeterDiagonal {
class Program {
static void Main(string[] args) {
//declare variables
double s, a, p, d;

// take input
Console.Write("Enter the length of side of the square: ");

// calculate area
a = s * s;
// calculate perimeter
p = 4 * s;
// calculate diagonal
d = s * Math.Sqrt(2);

// display result upto 3 decimal places
Console.Write("Area: " + string.Format("{0:0.000}", a) + "\n");
Console.Write("Perimeter: " + string.Format("{0:0.000}", p) + "\n");
Console.Write("Diagonal: " + string.Format("{0:0.000}", d) + "\n");

// wait for user to press any key
}
}
}


Output

Enter the length of side of the square: 5

Area: 25.000

Perimeter: 20.000

Diagonal: 7.071

• The program calculates the area, perimeter, and diagonal of a square based on the length of its sides.
• Four float variables are declared:
s: Stores the length of the side of the square.
a: Will store the area of the square.
p: Will store the perimeter of the square.
d: Will store the diagonal of the square.
• The program prompts the user to enter the length of the side of the square.
• It reads the user's input and stores it in the variable s.
• The program calculates the area of the square by squaring the side length (s * s), the perimeter by multiplying the side length by 4 (4 * s), and the diagonal by multiplying the side length by the square root of 2 (s * sqrt(2)).
• The calculated area, perimeter, and diagonal values are then displayed on the screen. They are formatted to show up to three decimal places.