Find Area of Square


Area of Square

To find the area of a square whose length of a side is given, use the following formula.

Area of Rectangle = side * side

Program

In the following program, we shall define a function areaOfSquare() that takes length of a side as argument and return the area of square.

We read length of a side as integer from user, call areaOfSquare() with the length of side passed as argument. You can also read length of the side as floating-point number from user using float() function. In the following program we use int() and you may try float() as practice.

Input

length of a side, where side > 0

Python Program

def areaOfSquare(side) :
    if side < 0 :
        print('Side cannot be negative.')
        return
    return side * side

side = int(input('Enter side length : '))
area = areaOfSquare(side)
print(f'Area of Square : {area}')

Output

Enter side length : 7
Area of Square : 49

Important Points regarding above program

  • input() function reads a string from standard input. Therefore, we have used int() function to convert string to integer.
  • If given length of side is less than zero, we are returning nothing.