How to find the perfect square elements in an array with Java

1 Answer

0 votes
public class MyClass {
    public static boolean is_perfect_square(double n) {   
       double sq = Math.sqrt(n); 
        
       return ((sq - Math.floor(sq)) == 0); 
    } 
     
    public static void main(String args[]) {
        int arr[] = { 7, 8, 9, 0, 36 };
        
        for (int i = 0; i < arr.length; i++) {
            if (is_perfect_square(arr[i])) 
                System.out.println(arr[i] + " : Yes"); 
            else
                System.out.println(arr[i] + " : No"); 
        }
    }
}
 
 
 
 
/*
run:
 
7 : No
8 : No
9 : Yes
0 : Yes
36 : Yes
 
*/

 



answered Sep 23, 2021 by avibootz

Related questions

1 answer 196 views
1 answer 173 views
1 answer 163 views
1 answer 187 views
1 answer 115 views
1 answer 114 views
...