Skip to content
Advertisement

How to find out if a number is a perfect square without using sqrt function or ** in Python? [duplicate]

I have to write a program that finds out whether or not a number is a perfect square. The terms are I don’t use a sqrt function or an exponent (**)

I previously showed my teacher my solution using exponent (**) and she told me not to include that there.

num=int(input("Enter a positive integer: "))
base=1
while num/base!=base:
    base=base+1
if (num/base)%1==0:
    print(num,"is a square")
else:
    print(num,"is not a square")

It works fine with perfect squares but when they’re not, it won’t work because I can’t find a way to get it out of the while loop even though it’s not a perfect square.

Advertisement

Answer

You have to change

while num/base!=base:

to

while num/base>base:

and it will work.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement