1

having trouble with perfectsquare code

import java.util.*;


public class perfectsquare {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

Scanner s= new Scanner(System.in); 
double number; 

System.out.println("Enter number >");

number= s.nextDouble();

System.out.println(Math.sqrt(number));





    }

}

Using if,else how could you make it return a perfect square like sqrt of 9=3 but false if the square root of that number is a decimal for example squareroot of 10 would be 3.122222.....

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
NBera
  • 329
  • 1
  • 3
  • 13

3 Answers3

3
if(Math.round(Math.sqrt(number))==Math.sqrt(number)){
  //it is a perfect square
}
Neil Locketz
  • 4,228
  • 1
  • 23
  • 34
  • 1
    `if (Math.sqrt(number)==Math.sqrt(number)%1)` – tckmn Feb 02 '13 at 02:24
  • You can also use `Math.ceil` or `Math.floor` or multiply the sqrt's truncation to `int` by itself and see if it was the original number (after a cast to `double`). – eboix Feb 02 '13 at 02:24
  • ^ This would work also, there is many ways to solve this problem this is just the first one that came to my head :P – Neil Locketz Feb 02 '13 at 02:28
2

You can change the return type to Object(But you will have to check if it's a number using instanceof when using the function) or return -1 instead of false

To see if it's a perfect square, use this:

if(Math.sqrt(number) % 1 == 0) {
    return number;
} else {
    return -1;
}
0
import java.util.*;

public class perfectsquare {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner s= new Scanner(System.in); 
        double number; 

        System.out.println("Enter number >");

        number= s.nextDouble();
        double sqr= Math.sqrt(number);
        //System.out.println(sqr);
        double num=sqr;
        double x;
        x=num%1;
        x=num-Math.floor(num);
        if(x>0){
            System.out.println("False");
        }
        else{
            System.out.println(sqr);
        }
    }
}

This will work.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
user1658435
  • 574
  • 1
  • 9
  • 28
  • Your logic seems faulty, I would do `% 1`. – eboix Feb 02 '13 at 02:27
  • In java it is not working. I'm new to java – user1658435 Feb 02 '13 at 02:31
  • Sorry, I wasn't clear enough. Earlier, I said that Java double mod operations were not well defined, but I take that back. They're not well defined in C, but they are well defined in Java. The reason this is not working is that you are doing `% 10` instead of `% 1`. Your statement will return whether the square root has a one's digit. `% 1` will get you whether or not it has decimals, which is what you want to know. Also, you should change your `double num=sqr; if(num ....` to just `if(sqr ....`. There isn't a reason to have an auxiliary variable `num`, there. – eboix Feb 02 '13 at 02:33
  • yaa i edited it, this program will work now :) – user1658435 Feb 02 '13 at 02:39
  • This would be more helpful if you formatted the code. Thanks. – Kurtymckurt Feb 02 '13 at 06:04