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.
JavaScript
x
9
1
num=int(input("Enter a positive integer: "))
2
base=1
3
while num/base!=base:
4
base=base+1
5
if (num/base)%1==0:
6
print(num,"is a square")
7
else:
8
print(num,"is not a square")
9
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
JavaScript
1
2
1
while num/base!=base:
2
to
JavaScript
1
2
1
while num/base>base:
2
and it will work.