To find the perimeter of a square, just add up all the lengths of the sides.
The area of a square: To find the area of a square, multiply the lengths of two sides together. Another way to say this is to say “square the length of a side.
In this example, I’ll show how to calculate area and perimeter of rectangle in C#.
Python Code:
1 2 3 4 5 6 7 | s=int(input("Side : ")) area=s*s perimeter=4*s print("Area of Rectangle : ",area) print("Perimeter of Rectangle : ",perimeter) |
This code defines a program in Python that calculates the area and perimeter of a square. The program begins by prompting the user to enter the side of the square using the input
function and storing the value in a variable s
. It then calculates the area of the square by multiplying s
by itself and assigns the result to a variable area
. It calculates the perimeter by multiplying s
by 4 and assigns the result to a variable perimeter
. Finally, the program prints the values of area
and perimeter
to the terminal using the print
function.
You may also like: Python Tutorial with Exercises(100+ Examples)
To run this code and see the output, you can follow these steps:
- Save the code to a file or type it into a text editor.
- Open a terminal or command prompt.
- Navigate to the directory where the code is saved.
- Run the command
python filename.py
, wherefilename.py
is the name of the file containing the code. - The program will prompt you to enter the side of the square. Type in a value for this variable and press enter.
- The program will then calculate the area and perimeter of the square and print the results to the terminal.
For example, if you enter 5 for the side of the square, the output will be:
1 2 3 4 5 | Area of Rectangle : 9 Perimeter of Rectangle : 12 |
[…] Find Area and Perimeter of a Square in Python […]