# Java Program to Check if a Given Number is Perfect Square

In this section, we have created a number of Java program to check if a given number is perfect square or not.

The perfect square or square number is a positive integer that is square of an integer. In other words, when we multiply two same numbers together, then the product that we get is called the perfect square. In short, it is the product of two positive equal integers or product of an integer with itself.

The property of the perfect square number is that it ends only with 0, 1, 4, 6, 9, and 25. The examples of perfect square are:

```49=7*7
100=10*10
625=25*25
```

In Java, we can use the following way to check if a number is perfect square or not.

• Using sqrt() method
• Using User-Defined Logic

## Using sqrt() Method

The approach, we have followed is:

1. First, find out the square root of the given number.
2. Calculate the floor value of the calculated square root.
3. Find the difference of the floor value with the square root that we have find in step 1.
4. At last, compare the value (that we get in step 3) with 0. If the value is equal to 0 the given number is perfect square, else not.

Let's understand the above stapes through an example.

Example: Check the number 324 is perfect square or not.

• The square root of 324 is 18.
• The floor value of the square root is 18.
• The difference of square root and floor value is 0.
• The difference is equal to 0. Hence, the given number is perfect square.

Let's implement the above steps in a Java program.

To find the square root and floor value of the number, we have used the following predefined methods of the Java Math class.

sqrt(): It is a static method of the Math class. We can call it directly by using the class name. It parses a parameter of type double. If we pass a number less than zero or NaN, it returns NaN. It returns a positive square root of a double value.

syntax:

floor(): It is also a static method of the Math class. It calculates the floor value of the number. It parses a parameter of type double. It returns the largest (closest positive integer) floating-point value that is less than or equal to the argument and is equal to a mathematical integer.

syntax:

In the following example, we have created a user-defined method named checkPerfectSquare(). It parses an argument of type double. The method contains the logic to check if a number is perfect square.

In the method, we have calculated the square root of the number by the Math.sqrt() method and store it in a variable named sqrt. After that, we have found the floor value of the calculated square root by the Math.floor() method. Along with this, we have found the difference between the two sqrt and floor value. The resultant of the difference compared with 0 and returns a Boolean value.

The returned value passed to the calling method (that is inside if statement). If the difference is equal to 0, the given number is perfect square, else not a perfect square.

CheckPerfectSquareExample1.java

Output 1:

```Enter any number: 8100
Yes, the given number is perfect square.
```

Output 2:

```Enter any number: 24
No, the given number is not perfect square.
```

## Using User-Defined Logic

In the following example, we have not used any predefined method of the Java Math class. We have implemented own logic to check the perfect square.

CheckPerfectSquareExample2.java

Output 1:

```Enter a number: 121
Yes, the given number is perfect square.
```

Output 2:

```Enter a number: 121
No, the given number is not perfect square.
```

In the following example, first we have calculated the remainder of the given number using the modulo operator. After that, we have compared the remainder with 2, 3, 7, and 8 using the Logical OR operator because the perfect square never ends with these numbers. If the number has the remainder 2, 3, 7, or 8, it cannot be the perfect square.

If the remainder of a number is 0, 1, 4, 6, and 9 it jumps to the for loop. For each iteration of i, we have calculated the i*i. If the square of i is equal to the entered number (n), the number is perfect square and it returns true, else returns false.

CheckPerfectSquareExample3.java

Output 1:

```Enter a number: 121
Is the number perfect square? true
```

Output 2:

```Enter a number: 167
Is the number perfect square? false
```

Let's see another logic.

CheckPerfectSquareExample4.java

Output 1:

```Enter a number: 121
121 is a perfect square.
```

Output 2:

```Enter a number: 143
143 is not a perfect square.
```

Let's see another logic to check if a given number is perfect square or not.

In the following program, we have taken the square root of the number that we have entered and type-cast it into int. After that, we take the square of the value to see whether it is same as the number given. If they are equal to the number, we got a perfect square number.

CheckPerfectSquareExample5.java

Output 1:

```Please enter an integer: 98
98 is not a perfect square number.
```

Output 2:

```Please enter an integer: 529
529 is a perfect square number.
```

Let's see another example with another logic.

In the following example, we have used the approach, given below:

1. Suppose, n is a number that we want to check.
2. Create a for loop that executes from 1 to n/2.
3. Calculate x=i*i, for each iteration.
4. The variable x has the following three conditions:
• If x=n, the given number is perfect square.
• If x>n, n is not a perfect square.
• Continue if the above two conditions are not true.

CheckPerfectSquareExample6.java

Output:

```50 is a perfect square number? false
361 is a perfect square number? true
```

Next TopicJava Programs

### Feedback   