Java Program to Check Whether a Number is a Perfect Square

In Java, checking whether a number is a perfect square or not is a common task. A number is called a perfect square if it can be expressed as the product of an integer with itself. For example, 16 is a perfect square because it is the result of multiplying 4 × 4.

In this article, we will learn how to write a Java program to check whether a given number is a perfect square.

Steps to Check If a Number is a Perfect Square

  1. Input the Number: First, we need to input the number that we want to check.
  2. Square Root Calculation: The square root of the number is taken.
  3. Integer Comparison: If the square root of the number is an integer, then the number is a perfect square. If it's not an integer, the number is not a perfect square.
  4. Output the Result: Based on the above check, we print whether the number is a perfect square or not.

Java Code to Check Whether a Number is a Perfect Square

import java.util.Scanner;

public class PerfectSquare {

    // Method to check whether the number is a perfect square
    public static boolean isPerfectSquare(int num) {
        // Calculate the square root of the number
        double squareRoot = Math.sqrt(num);
        
        // Check if the square root is an integer
        return squareRoot == Math.floor(squareRoot);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Ask the user to input a number
        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        // Call the method to check if the number is a perfect square
        if (isPerfectSquare(num)) {
            System.out.println(num + " is a perfect square.");
        } else {
            System.out.println(num + " is not a perfect square.");
        }

        sc.close();
    }
}

How the Code Works?

  1. Input: The program asks the user to input a number, which will be checked to see if it’s a perfect square.
  2. Square Root Calculation: The Math.sqrt() method is used to calculate the square root of the given number. The square root value is stored in a variable called squareRoot.
  3. Integer Comparison: We then use Math.floor() to check if the square root is an integer. If the square root is equal to the floor value of the square root, it means that the number is a perfect square.
  4. Output: Based on this check, we print whether the number is a perfect square or not.

Examples

Input

Enter a number: 25

Output

Input

Enter a number: 20

Output

Explanation

  • Perfect Square Example: In the first example, 25 is a perfect square because 25=5\sqrt{25} = 525โ€‹=5, which is an integer.
  • Non-Perfect Square Example: In the second example, 20 is not a perfect square because 20≈4.47\sqrt{20} \approx 4.4720โ€‹≈4.47, which is not an integer.

Conclusion

This Java program is a simple way to determine whether a number is a perfect square or not. By using the Math.sqrt() method and checking if the square root is an integer, we can easily solve this problem. This type of problem helps students get comfortable with using mathematical functions and conditionals in Java.

Checking for perfect squares is an important concept in many mathematical algorithms, and understanding this logic can be useful when solving more complex problems.


Similar Articles