Ways to Check for the Perfect Square
Let's discuss different methods to check if a given number is a perfect square in Java. We will talk about two common approaches: using user-defined logic and the built-in Math.sqrt() function.
1. Using User-Defined Logic
One way to check if a number is a perfect square is by using a loop to iterate from 1 to the given number and checking if the square of any integer equals the number.
For example :
public static boolean isPerfectSquare(int num) {
if (num < 0) {
return false;
}
for (int i = 1; i * i <= num; i++) {
if (i * i == num) {
return true;
}
}
return false;
}
In this code, we first check if the input number is negative. If it is, we immediately return false since perfect squares are always non-negative. Then, we start a loop from 1 and continue until i * i is less than or equal to the input number. Inside the loop, we check if i * i equals the input number. If it does, we have found a perfect square and return true. If the loop completes without finding a match, we return false.
2. Using Math.sqrt()
Java provides a built-in Math.sqrt() function that returns the square root of a given number. We can use this function to check if a number is a perfect square.
For example:
public static boolean isPerfectSquare(int num) {
if (num < 0) {
return false;
}
int sqrt = (int) Math.sqrt(num);
return sqrt * sqrt == num;
}
In this approach, we first check if the input number is negative and return false if it is. Then, we calculate the square root of the number using Math.sqrt() and cast it to an integer. Finally, we check if the square of the calculated square root equals the original number. If it does, the number is a perfect square, and we return true; otherwise, we return false.
Note: Both methods will correctly identify perfect squares, but the Math.sqrt() approach is generally more efficient, especially for larger numbers.
Using User-Defined Logic
This method involves iterating through a loop and checking if the square of any integer equals the given number.
Let’s see how we can implement this in code:
public static boolean isPerfectSquare(int num) {
if (num < 0) {
return false;
}
for (int i = 1; i * i <= num; i++) {
if (i * i == num) {
return true;
}
}
return false;
}
1. The method takes an integer `num` as input, which is the number we want to check for being a perfect square.
2. We start by checking if `num` is negative. If it is, we immediately return `false` since perfect squares are always non-negative. This check helps optimize the code by avoiding unnecessary iterations for negative numbers.
3. If `num` is non-negative, we proceed to the loop. The loop variable `i` starts from 1 and continues as long as `i * i` is less than or equal to `num`. This condition ensures that we only iterate up to the square root of `num`, reducing the number of iterations required.
4. Inside the loop, we check if `i * i` equals `num`. If it does, we have found a perfect square, and we return `true`. This means that `num` can be expressed as the square of an integer (`i`), making it a perfect square.
5. If the loop completes without finding a match, it means that no integer squared equals `num`. In this case, we return `false`, indicating that `num` is not a perfect square.
For example :
public class PerfectSquareExample {
public static boolean isPerfectSquare(int num) {
if (num < 0) {
return false;
}
for (int i = 1; i * i <= num; i++) {
if (i * i == num) {
return true;
}
}
return false;
}
public static void main(String[] args) {
int number = 16;
boolean isSquare = isPerfectSquare(number);
System.out.println(number + " is a perfect square: " + isSquare);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
16 is a perfect square: true
In this example, we pass the number 16 to the `isPerfectSquare` method. The method returns `true` since 16 is a perfect square (4 * 4 = 16).
Note: The user-defined logic approach provides an easy way to check for perfect squares. However, it may not be the most efficient method, especially for large numbers, as it involves iterating through a loop.
Using Sqrt()
Now, let's discuss a more efficient approach to check if a number is a perfect square using the built-in `Math.sqrt()` function in Java. This method calculates the square root of a given number and allows us to determine if the number is a perfect square.
Let’s see it’s the code implementation:
public static boolean isPerfectSquare(int num) {
if (num < 0) {
return false;
}
int sqrt = (int) Math.sqrt(num);
return sqrt * sqrt == num;
}
In this example :
1. The method takes an integer `num` as input, representing the number we want to check for being a perfect square.
2. We start by checking if `num` is negative. If it is, we return `false` since perfect squares are always non-negative. This check helps optimize the code by avoiding unnecessary calculations for negative numbers.
3. If `num` is non-negative, we proceed to calculate its square root using the `Math.sqrt()` function. The `Math.sqrt()` function returns a double value, so we explicitly cast it to an integer using `(int)`. This step truncates the decimal part of the square root if it exists.
4. Finally, we check if the square of the calculated square root (`sqrt`) equals the original number (`num`). If they are equal, it means that `num` is a perfect square, and we return `true`. Otherwise, we return `false`.
For example :
public class PerfectSquareExample {
public static boolean isPerfectSquare(int num) {
if (num < 0) {
return false;
}
int sqrt = (int) Math.sqrt(num);
return sqrt * sqrt == num;
}
public static void main(String[] args) {
int number = 25;
boolean isSquare = isPerfectSquare(number);
System.out.println(number + " is a perfect square: " + isSquare);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
25 is a perfect square: true
In this example, we pass the number 25 to the `isPerfectSquare` method. The method calculates the square root of 25 using `Math.sqrt()`, which returns 5. Since 5 * 5 equals 25, the method returns `true`, indicating that 25 is a perfect square.
Note: The `Math.sqrt()` function provides a more efficient way to check for perfect squares than the user-defined logic approach. It eliminates the need for iterating through a loop and directly calculates the square root. This approach is useful when you are dealing with large numbers.
However, it's important to note that the `Math.sqrt()` function returns a double value, which may introduce small precision errors due to floating-point arithmetic. Casting the result to an integer helps solving this issue in most cases.
Frequently Asked Questions
Can a negative number be a perfect square?
No, a negative number cannot be a perfect square. Perfect squares are always non-negative.
Is zero a perfect square?
Yes, zero is considered a perfect square since 0 * 0 equals 0.
What is the time complexity of checking for perfect squares using Math.sqrt()?
The time complexity of using Math.sqrt() is O(1) as it performs a constant-time operation.
Conclusion
In this article, we discussed the concept of perfect squares in Java. We learned what perfect squares are, their properties, and how to check if a number is a perfect square using user-defined logic and the Math.sqrt() function. With the implementation of these methods, you can efficiently work with perfect squares in your Java programs.
You can also check out our other blogs on Code360.