**Jump To Right Section**Show

Any number which can be expressed as the product of two whole equal numbers is classified as a perfect square. For example, 64 can be written as 8*8 hence 64 is a perfect square. In this article, we will create a Python program to check whether the number is a perfect square or not.

##### AlgorithmÂ To Check If A Number Is A Perfect Square Or Not

**Step 1**:Â Take the input from the user

**Step 2**:Â Compute the square root of the given number using the math library

**Step 3**: Checking whether theÂ int(root + 0.5) ** 2 == number, if this evaluates to True then the number is a perfect square

##### Python Program To Check If A Number Is A Perfect Square

1 2 3 4 5 6 7 8 9 10 11 12 |
import math # Taking the input from user number = int(input("Enter the Number")) root = math.sqrt(number) if int(root + 0.5) ** 2 == number: print(number, "is a perfect square") else: print(number, "is not a perfect square") |

##### Explanation

First, we are importing the math library in our program then we are taking the input from the user and converting it to integer just in case the user inputs a float number.

The square root of the number is calculated withÂ `math.sqrt`

the method and the result is stored inÂ Â `root`

a variable. Next, we are checking whether the integer value of theÂ square ofÂ `root+0.5`

Â is equal to the number itself if this evaluates to True, then the number is a perfect square else it is not.

Notice we are adding 0.5 to the root because in the case of a large number the root may not be a perfect whole number it might be some decimal less. However, as it is known the `int()`

Â method takes the floor value adding 0.5 to the float number is a reliable solution to get the desired outputs even if the input is large.

Running the program for some test cases gave the following output,

1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter the Number 444 444 is not a perfect square Enter the Number 64 64 is a perfect square Enter the Number 81 81 is a perfect square Enter the Number 998001 998001 is a perfect square |

## Leave a Comment