How to Print the Square of a Number in Python

How to Print the Square of a Number in Python | Whenever it comes to handling enormous amounts of data, Python is the most widely used programming language. Python has a number of built-in libraries and default methods that allow you to execute several operations on data efficiently and effectively. Squaring a number is an example of a multiplication operation where the integer is multiplied by itself. In this post, we’ll look at many ways to square an integer in Python, as well as an example and outputs. 

Squaring an integer is the same as multiplying it by itself. In Python, there are four methods for finding the squares of a number.

To print the square of a number in python, the first thing you can do is to multiply the number by itself. This is the most straightforward and straightforward technique for calculating the square of a number in Python.

a = 3
square = a * a
print(square)

Output:-

9

Same Program by defining a function

def square(a):
    return a*a


print(square(5))

Output:-

25

In Python, you can also use the exponent operator to get the squares of a specific number. “**” is the symbol for it. The exponent method outputs the exponential power resulting in the square of the number when using this method. It’s worth noting that “a**b” will be interpreted as “a to the power of b.” 

n = 10
result = n ** 2
print(result)

Output:-

100

Print the Square of a Number in Python Using the exponent function by taking input from the end-user

n = int(input("Enter a number: "))
result = n ** 2
print(result)

Output:-

Enter a number: 25
625

In Python, pow() is a function from the math package that may be used to find the square of a value. The pow() method can also be used to find various exponential powers of a given value.

The pow() method accepts a two-parameter argument, the first of which is the number, and the second of which is the number’s exponential power. The second argument will be “2” in our case because we want to find the square of the integer. For a greater grasp of the pow() method, look at the illustration below.

n = 11
square = pow(n, 2)
print(square)

Output:-

121

Let us see the above program by taking input from the end-user.

n = int(input("Enter a number: "))
square = pow(n, 2)
print(square)

Output:-

Enter a number: 25
625

As a result, we’ve offered several ways for finding the square of a given number explicitly using the fewest steps available. It is strongly advised that you study and apply these methods in order to make your code more efficient and speedier.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *