| by Arround The Web | No comments

How to Check if a Number is a Perfect Square in JavaScript

In JavaScript, we often encounter mathematical problems, including square roots. In such cases, perfect squares play a very important role as they can ease our calculations to a great extent. Also, as the square root of the perfect square is always a natural number, it makes our life easier in mathematics. Moreover, it saves us a lot of time dealing with complex calculations.

This write-up will guide you about checking if the entered number is a perfect square in JavaScript.

How to Check/Verify if the Provided Number is a Perfect Square in JavaScript?

To verify if a provided number is a perfect square in JavaScript, you can utilize:

The mentioned approaches will now be discussed one by one!

Method 1: Check if the Provided Number is a Perfect Square Using Math.sqrt() Method

The “Math.sqrt()” method results in the square root of a number. By utilizing it, you can also check the condition for the perfect square of the specific number with the help of the “if-else” loop.

Syntax

Math.sqrt(x)

Here, the Math.sqrt() method will output the square root of the “x” variable.

Let’s go through the following example for demonstration.

Example

First, we will prompt the user to input the number and store it in a variable named “perfectSquare”:

var perfectSquare= prompt("Enter a number:")

Now, we will get the square root of the provided number using the “Math.sqrt()” method:

z= Math.sqrt(perfectSquare)

After that, we will check the condition that if the provided number equals the product of the square root of itself, it will satisfy the perfect square condition:

if(perfectSquare == z*z){
     alert("The Number is a perfect square")
}
else{
   alert("The Number is not a perfect square")
}

Save the provided code and enter any number for verification:

The given output indicates that the entered number which is “9” is a perfect square:

Method 2: Check if a Number is a Perfect Square Using “Math.floor()” and “Math.ceil()” Methods

Math.floor()” method rounds down the specified number to an integer such as 4.7 will be rounded to 4, whereas the “Math.ceil()” method returns the rounded number up to an integer such as 4.7 will be rounded to 5. Both of these methods can be used at the same time to verify if the provided number is a perfect square or not.

Syntax

Math.floor(x), Math.ceil(x)

Here, “x” refers to an integer.

Look at the following example for a better understanding of the concept:

Example

Now, we will include the “Math.sqrt()” method in both of the above methods to handle the resultant square root value. Then, verify the provided number as a perfect square using the “Math.ceil()” and “Math.floor()” methods.

This combination will work in such a way that if the provided number is an imperfect square, the values of both methods will vary, and the “else” block will be executed. In the other case, the provided number will be a perfect square:

if(Math.ceil(Math.sqrt(perfectSquare)) == Math.floor(Math.sqrt(perfectSquare))){  
   alert("The Number is a perfect square")
}
else{
       alert('The Number is not a perfect square')
}

The output of the above implementation will result as follows:

We have compiled all the simplest methods for checking if a number is a Perfect Square in JavaScript. You can utilize any of the above methods according to your understanding.

Conclusion

To check if the entered number is a perfect square in JavaScript, use the “Math.sqrt()” method and add an if-else statement to check the perfect square condition. Also, the “Math.floor()” and “Math.ceil()” methods can be used in combination for applying the condition on the rounded number resulting from a square root. This write-up explained to verify if the provided number is a perfect square.

Share Button

Source: linuxhint.com

Leave a Reply