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

3.

4.

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

/*********************************** alphabetacoder.com C program to calculate the area ,perimeter and diagonal of a square ************************************/ #include <stdio.h> #include <math.h> int main() { // declare variables float s, a, p, d; // take input printf("Enter the length of side of the square: "); scanf("%f", & s); // calculate area a = s * s; // calculate perimeter p = 4 * s; // calculate diagonal d = s * sqrt(2); // display result upto 3 decimal places printf("Area: %0.3f\n", a); printf("Perimeter: %0.3f\n", p); printf("Diagonal: %0.3f\n", d); return 0; }

Output

**Case 1:**

Enter the length of side of the square: 7.25

Area: 52.563

Perimeter: 29.000

**Case 2:**

Enter the length of side of the square: 10

Area: 100.000

Perimeter: 40.000