Python program to calculate square of a given number


Square is the defined as the multiplication of the number by itself. The square of a number n is given as n2. Mathematically we can implement square of the number as follows.

$\mathrm{n^{2}=n*n}$

In the same way we can calculate the square of a given number by using python language too. There are few approaches in python to find the square of a number, let’s see them one by one.

Using exponential operator (**)

The exponential operator is the operator which is used to perform the exponent arithmetic operation. It is denoted with the symbol **.

Syntax

The following is the syntax to find the square of a number using the exponential operator.

$\mathrm{n^{2}=n**2}$

Example

In this example we are going to calculate the square of 25 by using the exponential operator (**).

n = 25
def exponential_operator(n):
   sqaure_n = n ** 2
   print("The square of",n,"is",sqaure_n)
exponential_operator(n)    

Output

The square of 25 is 625

Using Multiplication operator (*)

The square of a number can be calculated by multiplying the number with itself. Here to perform the multiplication we use * operator. The following is the syntax.

$\mathrm{n^{2}=n*n}$

Example

Here we are trying to find the square of 87 by using the multiplication operator (*).

n = 87
def multiplication_operator(n):
   square_n = n * n
   print("The square of",n,"calculated using multiplication operator is",square_n)
multiplication_operator(n)  

Output

The square of 87 calculated using multiplication operator is 7569

Using Math module

Math module is the built-in module available in python to perform the mathematical tasks. There are different functions available in math module such as pow(), log(), cos() etc.

The pow() function takes two input arguments, one is the number and the other is the power of the number to be calculated and then returns the power output

Syntax

The following is the syntax of the math.pow() function.

import math
n2 = pow(number,power)

Example

In this example we are going to calculate the square of 14 by passing 14 and 2 as the input parameters of the pow() function of the math module then it returns the square of 14.

import math
n = 14
p = 2
def power(n,p):
   square_n = math.pow(n,p)
   print("The square of",n,"calculated using multiplication operator is",square_n)
power(n,p)

Output

The square of 14 calculated using multiplication operator is 196.0

Updated on: 19-Oct-2023

96 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements