Answer:
- public class Square {
- public static boolean isPerfectSquare(int n){
- int sqrt_n = (int) Math.sqrt(n);
- if(sqrt_n * sqrt_n == n ){
- return true;
- }else{
- return false;
- }
- }
- }
Step-by-step explanation:
Firstly, use sqrt method from Math package to calculate the square root of input n (Line 3). Cast the result to integer and assign it to sqrt_n variable.
Next, square the sqrt_n and check if it is equal to input n (Line 4). If so, return true, if not, return false (Line 4-8).