9

How could I write an if-then statement that checks if an inputted integer is a perfect square or not (i.e. if I took the square root, it would be an integer as well: 4, 9, 16, 25, 36, etc.) in DrJava? Thank you!

Brooke
  • 103
  • 1
  • 1
  • 3

1 Answers1

33

I am aware that this question already has an answer.... But just in case, this also works.

int x = (int) Math.sqrt(input);
if(Math.pow(x,2) == input)
    //Do stuff
Community
  • 1
  • 1
Jaskaranbir Singh
  • 2,034
  • 3
  • 17
  • 33