Check Perfect Square in Java
What you’ll learn
- How to verify perfect squares using integer square root checks.
- How to handle negatives, zero, and one correctly.
- How to print perfect squares in a range using Java loops.
What is a perfect square?
A number is a perfect square if it equals k * k for some integer k.
Examples: 1, 4, 9, 16, 25. Non-examples: 2, 3, 5, 6, 7.
Prerequisites
Integer arithmetic, loop basics, and square-root intuition.
- Integer arithmetic and loops.
- Understanding of
Math.sqrtand casting.
The idea
Compute root = floor(sqrt(n)). If root * root == n, it is a perfect square.
Intuition
15 lies between 3^2=9 and 4^2=16, so not a square.
Live preview
Enter a number and check if it is a perfect square.
Algorithm
- If
n < 0, return false. - Compute integer root:
int root = (int) Math.sqrt(n). - If
root * root == n, it is a perfect square; otherwise not.
📜 Pseudocode
Pseudocode
function isPerfectSquare(n):
if n < 0:
return false
root = floor(sqrt(n))
return root * root == n1
Check one number
This program checks whether 16 is a perfect square.
java
public class Main {
static boolean isPerfectSquare(int number) {
if (number < 0) return false;
int root = (int) Math.sqrt(number);
return root * root == number;
}
public static void main(String[] args) {
int number = 16;
if (isPerfectSquare(number)) {
System.out.println(number + " is a perfect square.");
} else {
System.out.println(number + " is not a perfect square.");
}
}
}2
Perfect squares in a range
This prints perfect squares between 1 and 50.
java
public class Main {
static boolean isPerfectSquare(int number) {
if (number < 0) return false;
int root = (int) Math.sqrt(number);
return root * root == number;
}
public static void main(String[] args) {
System.out.println("Perfect squares between 1 and 50:");
for (int i = 1; i <= 50; i++) {
if (isPerfectSquare(i)) {
System.out.print(i + " ");
}
}
}
}Optimization note
For integer-only checks on huge values, binary search root in O(log n).
❓ FAQ
A number n is a perfect square if there exists an integer k such that k * k = n.
Yes. 1 = 1 * 1.
Yes. 0 = 0 * 0.
Not in real integers. So for this tutorial, negatives return false.
Math.sqrt gives a floating value; converting to int and re-checking root * root avoids classification mistakes.
Looping up to sqrt(n) takes O(sqrt(n)) time and O(1) extra space.
🔄 Input / output examples
| n | Result |
|---|---|
16 | Perfect square |
15 | Not a perfect square |
1 | Perfect square |
Edge cases
Zero/One
Both are perfect squares
0 = 0^2 and 1 = 1^2.
Negative
Not perfect squares in integers
Negative numbers have no real integer square root.
⏱️ Time and space complexity
| Method | Time | Extra space |
|---|---|---|
| Square-root check | O(1) (single check) | O(1) |
| Integer loop up to sqrt(n) | O(√n) | O(1) |
Summary
- Check
root = floor(sqrt(n))and verifyroot*root == n. - Guard negatives early.
Did you know?
Perfect squares are 0, 1, 4, 9, 16, 25, 36... and the differences are odd numbers: 1, 3, 5, 7, 9...
8 people found this page helpful
