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.
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
Diagonal: 10.253
Case 2:
Enter the length of side of the square: 10
Area: 100.000
Perimeter: 40.000
Diagonal: 14.142
- The program calculates the area, perimeter, and diagonal of a square based on the length of its sides.
- It includes two header files:
for input and output operations and for mathematical functions. - 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.
- The main function returns 0, indicating successful program execution.