Kodekraftt
/Blog
/Check Perfect Square In Java Without Using Mathsqrt Explanation Logic Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Check Perfect Square in Java Without Using Math.sqrt() – Explanation, Logic, Examples, and Program

Learn how to check whether a number is a Perfect Square in Java without using Math.sqrt(). This guide explains the logic, provides examples, and includes an efficient program using iterative checking.

Check Perfect Square in Java Without Using Math.sqrt()

A Perfect Square is a number that can be expressed as the square of an integer. Usually, we check perfect squares using Math.sqrt(), but sometimes interviewers ask you to do it without using any square root function.


Approach Without Math.sqrt()

To determine whether a number is a perfect square without using Math.sqrt(), we can:

  • Run a loop from 1 up to the number
  • Square each number
  • If any square matches the original number → it is a Perfect Square

This method is simple, reliable, and often asked in interviews.


Example

Check if 36 is a Perfect Square

  • 1 × 1 = 1
  • 2 × 2 = 4
  • 3 × 3 = 9
  • 4 × 4 = 16
  • 5 × 5 = 25
  • 6 × 6 = 36 ✔

Since 6² = 36, the number 36 is a Perfect Square.


Java Program (Without Using Math.sqrt)


public class PerfectSquareWithoutSqrt {

    // Function to check perfect square without Math.sqrt()
    static boolean isPerfectSquare(int num) {
        if (num < 0) return false; // negative numbers are never perfect squares
        
        for (int i = 1; i * i <= num; i++) {
            if (i * i == num) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int num = 36;

        if (isPerfectSquare(num)) {
            System.out.println(num + " is a Perfect Square");
        } else {
            System.out.println(num + " is NOT a Perfect Square");
        }
    }
}

Why This Works

Instead of calculating the square root, we check every number starting from 1 and square it. As soon as the square exceeds the target number, the loop stops—making it efficient.

For example, if the number is 10000, the loop runs only up to 100 (its approximate root).


Practice Challenges

  1. Write a program to print all perfect squares between 1 and 1000 without using Math.sqrt().
  2. Create a function that returns the nearest perfect square for any given number.
  3. Given an array of numbers, print only those which are perfect squares (without using Math.sqrt()).

You might also like

Disarium Number in Java: Definition, Examples, and Complete Program

A detailed explanation of Disarium Numbers in Java with definition, examples, step-by-step logic, and a complete Java program. Ideal for beginners, students, and coding interview preparation.

Perfect Square in Java with Explanation, Definition, Examples, and Program

A Perfect Square is a number that can be expressed as the square of an integer. This guide explains the concept with examples and provides a simple Java program to check if a number is a perfect square.

Fascinating Number in Java with Explanation, Definition, Examples, and Program

A Fascinating Number is a number which, when multiplied by 2 and 3 and then concatenated with itself, forms a string that contains all digits from 1 to 9 exactly once.

Find Square Root of a Number in Java Without Using Math.sqrt() – Explanation, Logic, Examples, and Program

Learn how to calculate the square root of any number in Java without using Math.sqrt(). This guide explains the binary search method, includes examples, and provides a clean Java program.

Spy Number in Java — Definition, Logic, Examples, and Program

Learn what a Spy Number is in Java with clear explanations and examples. This blog covers the definition, logic, step-by-step approach, and a clean Java program to check whether a number is a Spy Number. Includes practice challenges to improve your number theory skills.

Automorphic Number in Java: Definition, Explanation, and Step-by-Step Program

A clear and detailed guide on Automorphic Numbers in Java, including definition, examples, logic breakdown, and a complete Java program. Ideal for beginners, students, and coding interview preparation.