Thursday, December 26, 2024
Google search engine
HomeLanguagesPython program to calculate square of a given number

Python program to calculate square of a given number

Given a number, the task is to write a Python program to calculate square of the given number.

Examples:

Input: 4
Output: 16

Input: 3
Output: 9

Input: 10
Output: 100

We will provide the number, and we will get the square of that number as output. We have three ways to do so:

  • Multiplying the number to get the square (N * N)
  • Using Exponent Operator
  • Using math.pow() Method

Method 1: Multiplication

In this approach, we will multiply the number with one another to get the square of the number.

Example:

Python3




# Declaring the number.
n = 4
 
# Finding square by multiplying them
# with each other
square = n * n
 
# Printing square
print(square)


Output:

16

Method 2: Using Exponent Operator

In this approach, we use the exponent operator to find the square of the number.

Exponent Operator: **

Return: a ** b will return a raised to power b as output.

Example:

Python3




# Declaring the number.
n = 4
 
# Finding square by using exponent operator
# This will yield n power 2 i.e. square of n
square = n ** 2
 
# Printing square
print(square)


Output:

16

Method 3: Using pow() Method

In this approach, we will use the pow() method to find the square of the number. This function computes x**y and returns a float value as output.

Syntax: float pow(x,y)

Parameters :

x : Number whose power has to be calculated.

y : Value raised to compute power.
 

Return Value : 
Returns the value x**y in float.

Example:

Python3




# Declaring the number.
n = 4
 
# Finding square by using pow method
# This will yield n power 2 i.e. square of n
square = pow(n, 2)
 
# Printing square
print(square)


Output:

16.0

Method 4 : Using operator.pow() method

Approach

  1. Declaring number
  2. Raised each element to power 2 using operator.pow() and assigned to output variable
  3. Displayed output 

Python3




# Declaring the number.
n = 4
 
# Finding square by using pow method
import operator
square = operator.pow(n, 2)
 
# Printing square
print(square)


Output

16

Time Complexity : O(N)
Auxiliary Space : O(1)

Method to find the square of a number using bitwise operators:

This method uses the bitwise left shift operator (<<) to multiply the number by 2, effectively finding the square of the number.

Python3




# Declaring the number.
n = 4
 
# Finding square using bitwise left shift operator
square = n << 1
 
# Printing square
print(square)


Output

8

Time complexity: O(1)
Auxiliary space: O(1) as it only uses a single variable to store the result.

RELATED ARTICLES

Most Popular

Recent Comments