Learn about Perfect Square in Python without using sqrt() method in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Perfect Square in Python without using sqrt() method
# Program to check perfect square without sqrt()
#user-defined function
def PerfectSquare(x) :
i = 1
while(i**2 <= x):
if ((x%i == 0) and (x/i == i)):
return True
i += 1
return False
# input from user
num = int(input('Enter number: '))
# function call
if (PerfectSquare(num)):
print(num, 'is a perfect square')
else:
print(num, 'is not a perfect square')
Output:
Enter number: 4
4 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 : Check if Number is Perfect Square in Python using Function