Python program to calculate square of a given number

A square is defined as the multiplication of a number by itself. The square of a number n is given as n2. Mathematically, we can represent the square of a number as:

n² = n × n

Python provides several approaches to calculate the square of a number. Let's explore the most common methods.

Using Exponential Operator (**)

The exponential operator (**) performs exponent arithmetic operations. To find the square, we raise the number to the power of 2.

Syntax: n ** 2

Example

Here we calculate the square of 25 using the exponential operator ?

n = 25

def calculate_square_exponential(n):
    square_n = n ** 2
    print("The square of", n, "is", square_n)

calculate_square_exponential(n)
The square of 25 is 625

Using Multiplication Operator (*)

The most straightforward approach is multiplying the number by itself using the multiplication operator (*).

Syntax: n * n

Example

Here we find the square of 87 using the multiplication operator ?

n = 87

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

calculate_square_multiplication(n)
The square of 87 calculated using multiplication is 7569

Using Math Module

The math module provides the pow() function for power calculations. It takes two arguments: the base number and the exponent.

Syntax: math.pow(number, power)

Example

Here we calculate the square of 14 using math.pow() ?

import math

n = 14
p = 2

def calculate_square_math(n, p):
    square_n = math.pow(n, p)
    print("The square of", n, "calculated using math.pow() is", square_n)

calculate_square_math(n, p)
The square of 14 calculated using math.pow() is 196.0

Comparison

Method Syntax Return Type Performance
Exponential (**) n ** 2 int/float Fast
Multiplication (*) n * n int/float Fastest
Math.pow() math.pow(n, 2) float Slower

Conclusion

The multiplication operator (*) is the most efficient method for calculating squares. Use the exponential operator (**) for readability, and math.pow() when working with floating-point precision requirements.

---
Updated on: 2026-03-27T15:47:32+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements