Java Program to Check if a Number is a Perfect Square

Introduction

A perfect square is a number that can be expressed as the product of an integer with itself. For example, 16 is a perfect square because it is equal to 4 × 4. This guide will walk you through writing a Java program that checks whether a given number is a perfect square.

Problem Statement

Create a Java program that:

  • Prompts the user to enter an integer.
  • Checks if the integer is a perfect square.
  • Displays whether the number is a perfect square or not.

Example:

  • Input: 16

  • Output: "16 is a perfect square."

  • Input: 20

  • Output: "20 is not a perfect square."

Solution Steps

  1. Read the Number: Use the Scanner class to take an integer as input from the user.
  2. Check if the Number is a Perfect Square: Use the Math.sqrt() function to find the square root of the number and check if it is an integer.
  3. Display the Result: Print whether the number is a perfect square.

Java Program

// Java Program to Check if a Number is a Perfect Square
// Author: https://www.rameshfadatare.com/

import java.util.Scanner;

public class PerfectSquareChecker {
    public static void main(String[] args) {
        // Step 1: Read the number from the user
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter a number: ");
            int number = scanner.nextInt();
            
            // Step 2: Check if the number is a perfect square
            if (isPerfectSquare(number)) {
                System.out.println(number + " is a perfect square.");
            } else {
                System.out.println(number + " is not a perfect square.");
            }
        }
    }
    
    // Method to check if a number is a perfect square
    public static boolean isPerfectSquare(int number) {
        if (number < 0) {
            return false;
        }
        int sqrt = (int) Math.sqrt(number);
        return (sqrt * sqrt == number);
    }
}

Explanation

Step 1: Read the Number

  • The Scanner class is used to read an integer input from the user. The nextInt() method captures the input number.

Step 2: Check if the Number is a Perfect Square

  • The isPerfectSquare() method determines whether the number is a perfect square:
    • The method first checks if the number is negative. Negative numbers cannot be perfect squares, so the method returns false if the number is negative.
    • The square root of the number is calculated using Math.sqrt(), and the result is cast to an integer.
    • The method then checks if squaring the integer square root equals the original number. If it does, the number is a perfect square.

Step 3: Display the Result

  • The program prints whether the number is a perfect square using System.out.println().

Output Example

Example 1:

Enter a number: 16
16 is a perfect square.

Example 2:

Enter a number: 20
20 is not a perfect square.

Example 3:

Enter a number: 25
25 is a perfect square.

Conclusion

This Java program demonstrates how to check if a given number is a perfect square. It covers essential concepts such as using the Math.sqrt() function, conditional statements, and user input handling, making it a valuable exercise for beginners learning Java programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top