Check Perfect Square in Java

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
Square-root check

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.sqrt and 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.

Live result
Press "Run check".

Algorithm

  1. If n < 0, return false.
  2. Compute integer root: int root = (int) Math.sqrt(n).
  3. 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 == n
1

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

nResult
16Perfect square
15Not a perfect square
1Perfect 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

MethodTimeExtra space
Square-root checkO(1) (single check)O(1)
Integer loop up to sqrt(n)O(√n)O(1)

Summary

  • Check root = floor(sqrt(n)) and verify root*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...

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful