Check Perfect Square
Check whether a number is a perfect square.
BeginnerTopic: Conditional Programs
Python Check Perfect Square Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to check if a number is a perfect square
import math
num = int(input("Enter a non-negative integer: "))
if num < 0:
print("Negative numbers cannot be perfect squares in real numbers.")
else:
root = int(math.sqrt(num))
if root * root == num:
print(num, "is a perfect square")
else:
print(num, "is not a perfect square")Output
Enter a non-negative integer: 16 16 is a perfect square
Understanding Check Perfect Square
We take the integer square root and square it back.
If root * root equals the original number, it is a perfect square.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.