Linux, OpenSource, Programming And Hacks

Jan 10, 2018

Python Program to check if a number is Perfect Square

Jan 10, 2018
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))
  print("Not Perfect Square")

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

No comments:

Post a Comment