Find square of a number in Python
In this post, you will learn different ways to find a square of a number using the Python programming language. Such a type of question can improve your logical problem solving skills.
In mathematics, the square of a number is the result of multiplying that number by itself, and the multiplication is done only one, like a x a. For example, if 3 is a number, then the square of that number is 3 x 3, i.e., 9, the same as raising the number to the power of 2 (32).
Python program to find square using arithmetic operator
In the given Python program, we allowed the user to enter any number input using the Python input() method and then used the arithmetic operator to find the square of the given input.
# Python program to calculate square of a number
num = float(input("Please enter any numeric value: "))
square = num * num
print("The square of a given number {0} = {1}".format(num, square))
Output1
Please enter any numeric value: 81
The square of a given number 81.0 = 6561.0
Output2
Please enter any numeric value: 9
The square of a given number 9.0 = 81.0
Python program to find square using function
Here, we are finding the square of a number using the user-defined function square().
# Python program for square of a number
def square(num):
return num * num
num = float(input("Please enter any numeric value: "))
sqre = square(num)
print("The square of a given number {0} = {1}".format(num, sqre))
Output1
Please enter any numeric value: 4
The square of a given number 4.0 = 16.0
Output2
Please enter any numeric value: 5
The square of a given number 5.0 = 25.0
Python program to find square using Exponent operator
The exponent operator (**) returns the result of raising the first operand to the power of the second operand. In Python, we can use this exponent symbol to calculate the cube root of a number. It is also called a power operator. The given Python program calculates the square of a number using the exponent operator.
# get user input
num = int(input("Please enter any numeric value: "))
# calculating square using exponent operator
square = num**2
# print
print(f"The square of a given number {num} = {square}")
Output1
Please enter any numeric value: 4
The square of a given number 4 = 16
Python program to find square using the pow() function
The pow() function of Python returns the power of a number. This function accepts a number as the first argument and the exponent or power of the number as the second argument. The second argument is always 2, for finding the square of a number. To utilise, the pow() method, we need to import the math library into the program and then import the pow() function from the math module.
from math import pow
# input a number
num = int(input("Please enter any numeric value: "))
# calculating square
square = int(pow(num, 2))
# print
print(f"The square of a given number {num} = {square}")
Output1
Please enter any numeric value: 71
The square of a given number 71 = 5041
Python program to find square using the while loop
In the given Python program, we have used the while loop to find the square of a number. The while loop repeats the code till the condition becomes false.
num = 1
while num <= 8:
print(num, '\t', num ** 2)
num += 1
Output of the above code:
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
Related Articles
Find the stop words in nltk Python
Install NLTK for Python on Windows 64 bit
Python program to input week number and print week day
Python program to list even and odd numbers of a list
Python program to print odd numbers within a given range
Python program to multiply two numbers
Program to find area of triangle in Python
Find area of rectangle in Python
Swapping of two numbers in Python
Find average of n numbers in Python
Print multiplication table in Python
Python program to multiply two matrices
Python program to find area of circle
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Count consonants in a string Python
Convert array to list Python