---
title: "Write a program to check if a given number is a perfect square or not."  
description: "Write a program to check if a given number is a perfect square or not."  
author: "Revati S Misra"  
published: 2023-04-19  
updated: 2023-05-02  
canonical: https://www.mindstick.com/forum/157935/write-a-program-to-check-if-a-given-number-is-a-perfect-square-or-not  
category: "java"  
tags: ["java", "programs"]  
reading_time: 2 minutes  

---

# Write a program to check if a given number is a perfect square or not.

Write a [program to check](https://www.mindstick.com/forum/157542/write-a-java-program-to-check-if-a-list-of-integers-contains-only-odd-numbers) if a given number is a perfect square or not.

## Replies

### Reply by Aryan Kumar

```java
public class PerfectSquareChecker {

   public static boolean isPerfectSquare(int num) {
       int sqrt = (int) Math.sqrt(num);
       return sqrt * sqrt == num;
   }

   public static void main(String[] args) {
       int num = 16;
       if (isPerfectSquare(num)) {
           System.out.println(num + " is a perfect square");
       } else {
           System.out.println(num + " is not a perfect square");
       }
   }
}
```

In this program, we define a method **isPerfectSquare** that takes an integer **num** as input and returns a boolean value indicating whether or not **num** is a perfect square.

To [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) **num** is a perfect square, we calculate the square root of **num** using the **Math.sqrt** method, cast it to an integer, and assign it to the variable **sqrt**. If **sqrt * sqrt** is equal to **num**, then **num** is a perfect square and we return **true**. Otherwise, we return **false**.

In the **main** method, we test the **isPerfectSquare** method with the number 16 and print out a message indicating whether or not 16 is a perfect square. You can replace 16 with any other number to test the program with a different input.

### Reply by Krishnapriya Rajeev

The following Java [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) checks whether the given number is a perfect square or not:

```java
public class CheckPerfectSquare {
   public static void main(String[] args) {
      int num = 25;

       if (isPerfectSquare(num))
      {
            System.out.println(num + " is a perfect square.");
       }
       else
      {
            System.out.println(num + " is not a perfect square.");
       }
   }
   public static boolean isPerfectSquare(int num)
   {
       int sqrt = (int) Math.sqrt(num);
       return sqrt * sqrt == num;
   }
}

 Output = Enter a number: 25
            25 is a perfect square.
```


---

Original Source: https://www.mindstick.com/forum/157935/write-a-program-to-check-if-a-given-number-is-a-perfect-square-or-not

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
