In this tutorial, we will learn how to do a simple program of calculating the area of a square in python. The area is defined as the total space a shape occupies. It is measured in square units like cm², m², km² depending on the unit of the dimensions. The formula for calculating the area of a square is given as
The formula for Area of Square
Area of a square= Side x Side
Area= Side²
For example, following as input and give the output accordingly.
Input- 2.5
Output- 6.25
Input- 5
Output- 25
Here are two simple methods for calculating and printing the area of a square where the measurement of a side is given by the user.
- Using multiplication operator [*]
- Using pow[] function
Approach 1: Using multiplication operator [*]
Given below is a simple program for calculating area using the multiplication operator [*]. The input is taken as float and the area is calculated up to 4 decimal places. We will use the "%.4f" specifier for getting 4 digits after the decimal point. In "%.4f" the number after the dot is used to indicate the decimal places and f specifies float.
Algorithm
Step 1- Take input of side from user
Step 2 - Calculate area
Step 3- Print area using "%.4f"
Python Program
Look at the program to understand the implementation of the above-mentioned approach.
#area of square
s=float[input["Enter side of square"]]
area=s*s
print["Area of square=",'%.4f'%area]
Enter side of square3.2
Area of square= 10.2400
Approach 2: Using pow[] function
pow[] is a predefined math function in python which returns the value of x to the power y. To know more about pow[] and other built-in math functions. I advise you to read the article on Python math function.
Algorithm
Step 1- Define a function area_square[] to calculate area Take input of side from user
Step 2 - Call pow[] and set parameters as n,2 to calculate the area
Step 3- Take input from the user
Step 4- Call area_square[] and pass input as a parameter
Step 5- Print the area
Python Program
Look at the program to understand the implementation of the above-mentioned approach.
def area_square[n]:
area = pow[n,2]
return area
num=float[input["Enter number"] ]
print["Sum of digits",area_square[num]]
Enter side of square2.4
Area of square= 5.7600
Conclusion
In this tutorial, we learned how to calculate the area of a square using 2 approaches. One, by using simple statements for multiplication and printing the output. Two, using a predefined math function called pow[]. You can also define a function to calculate area by simply using the code from the first approach.
This article is created to cover some programs in Python, to find and print area of a square based on the length of its side, entered by user at run-time. Here are the list of programs:
- Find Area of Square without Function
- Find Area of Square using User-defined Function
- Using Class and Object
Formula to Find Area of Square
To find area of a square, use:
Here area indicates to area value and len indicates to length value of square
Find Area of Square
To find area of a square in Python, you have to ask from user to enter the side length. Now use the formula to find and print the area value as shown in the program given below:
print["Enter the Side Length of Square: "] l = float[input[]] a = l*l print["\nArea = ", a]
Here is the initial output produced by this program:
Now supply the input say 6 as side length of square to find and print its area based on the given value of its side length as shown in the snapshot given below:
Find Area of Square using Function
This program is created using a user-defined function named areaOfSquare[]. This function takes side length as its argument and returns the area value. The return value of this function gets initialized to a. Now print the value of a as output. That's it:
def areaOfSquare[s]: return s*s print["Enter the Side Length of Square: ", end=""] l = float[input[]] a = areaOfSquare[l] print["\nArea = {:.2f}".format[a]]
Here is its sample run with user input, 43.23 as side length of square:
Find Area of Square using Class
This is the last program on finding area of a square, using class and object, an object-oriented feature of Python. The program is little similar to the program created using function. The only difference is, we've to call the member function areaOfSquare[] of class using its object through dot [.] operator.
Note - Before accessing any member function of a class using an object say obj, the properties of class must be assigned to it. Therefore through the statement, obj = CodesCracker[], we've done the job.
class CodesCracker: def areaOfSquare[self, s]: return s*s print["Enter the Side Length of Square: ", end=""] l = float[input[]] obj = CodesCracker[] a = obj.areaOfSquare[l] print["\nArea = {:.2f}".format[a]]
This program produces the same output as of previous program.
Same Program in Other Languages
- Java Calculate Area of Square
- C Calculate Area of Square
- C++ Calculate Area of Square
Python Online Test
« Previous Program Next Program »