Python Program to Calculate Area, Perimeter and Diagonal of a Square

Area Perimeter and Diagonal of Square

Python 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. Python Program to calculate the area, perimeter and diagonal of a square

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

# take input
s = float(input("Enter the length of side of the square: "))

# calculate area
a = s * s
# calculate perimeter
p = 4 * s
# calculate diagonal
d = s * (2**0.5)

# display result upto 3 decimal places
print("Area:", round(a, 3))
print("Perimeter:", round(p, 3))
print("Diagonal:", round(d, 3))

Output


Enter the length of side of the square: 1

Area: 1.0

Perimeter: 4.0

Diagonal: 1.414



    This code calculates and displays various properties of a square based on the length of its side.
  1. The user is prompted to enter the length of the side of the square using the input() function. The value is converted to a floating-point number using float() and assigned to the variable s.
  2. The area of the square is calculated by multiplying the length of the side (s) by itself (s). The result is stored in the variable a.
  3. The perimeter of the square is calculated by multiplying the length of the side (s) by 4. The result is stored in the variable p.
  4. The diagonal of the square is calculated by multiplying the length of the side (s) by the square root of 2 (2**0.5). The result is stored in the variable d.
  5. The print() function is used to display the calculated values. The round() function is used to round each value to 3 decimal places before displaying it.
    • The line print("Area:", round(a, 3)) displays the calculated area.
    • The line print("Perimeter:", round(p, 3)) displays the calculated perimeter.
    • The line print("Diagonal:", round(d, 3)) displays the calculated diagonal.
    Overall, this code takes the length of a square's side as input, calculates its area, perimeter, and diagonal, and displays the results rounded to 3 decimal places.