Python Program to check if a number is Perfect Square

A number that can be expressed as the product of two equal integers is a Perfect Square number.

for example: 25 can be expressed as 5 x 5

In the given python3 code we take a user input and take out the square root and convert it into integer. if square root value multiplied by itself equals the user given number then its proved to be a Perfect number else not. 

import math

n = int(input())

i = int(math.sqrt(n)) #sqrt function returns float so typecasting to int

if(n == i*i):
print("perfect Square: %d * %d = %d"%(i,i,n))
else:
print("Not Perfect Square")

Please comment below if you have a better algorithm than the given one.

You can take this Python Quiz

Leave a Reply

Your email address will not be published.