Skip to content
Home » Python Program to Check Whether a Number is a Perfect Square

Python Program to Check Whether a Number is a Perfect Square

Learn about Python Program to Check Whether a Number is a Perfect Square in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Python Program to Check Whether a Number is a Perfect Square

# Program to check number is perfect square or not

#importing math module
import math  

# inputs from user
num = int(input('Enter number: '))

root = math.sqrt(num)

# check number is perfecct square
if int(root + 0.5) ** 2 == num:
    print(num, 'is a perfect square')
else:
    print(num, 'is not a perfect square')

Output:

Enter number: 9
9 is a perfect square

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Swap two numbers in Python using XOR operator