JavaScript Program to Find the Square Root
Last Updated :
13 Sep, 2023
Given a number N, the task is to calculate the square root of a given number using JavaScript. There are the approaches to calculate the square root of the given number, these are:
Approaches to Find the Square Root of Given Number in JavaScript:
- Using Math.sqrt() Method
- Using Math.pow() Method
- Using Binary Search
Basic Examples:
Input: num = 25
Output: 2
Explanation: The square root of 25 is 5.
Input: num = -120
Output: NaN
Explanation: The square root of Negative numbers is NaN.
Input: num = 'Geeks'
Output: NaN
Explanation: The square root of String Value is NaN.
The Math.sqrt() method is used to find the Square Root of a given number.
Syntax:
Math.sqrt( value )
Example: In this example we will find the square root using JavaScript Math.sqrt() method.
Javascript
const num1 = 25;
const num2 = -120;
const num3 = 'Geeks' ;
console.log( 'Square Root of ' +
num1 + ' is ' + Math.sqrt(num1));
console.log( 'Square Root of ' +
num2 + ' is ' + Math.sqrt(num2));
console.log( 'Square Root of ' +
num3 + ' is ' + Math.sqrt(num3));
|
Output
Square Root of 25 is 5
Square Root of -120 is NaN
Square Root of Geeks is NaN
The Math.pow() method is used calculate the power a number i.e., the value of the number raised to some exponent. To get the square root of any Number, we set the power to 1/2 of that number.
Syntax:
Math.pow( num, 1/2 )
Example: In this example, we will calculate the square root of given values using Math.pow() method.
Javascript
const num1 = 25;
const num2 = -120;
const num3 = 'Geeks' ;
console.log( 'Square Root of ' +
num1 + ' is ' + Math.pow(num1, 1/2));
console.log( 'Square Root of ' +
num2 + ' is ' + Math.pow(num2, 1/2));
console.log( 'Square Root of ' +
num3 + ' is ' + Math.pow(num3, 1/2));
|
Output
Square Root of 25 is 5
Square Root of -120 is NaN
Square Root of Geeks is NaN
Approach 3: Using Binary Search
The binary search is used to find the square root of the given number N with precision upto 5 decimal places.
- The square root of number N lies in range 0 ? squareRoot ? N. Initialize start = 0 and end = number.
- Compare the square of the mid integer with the given number. If it is equal to the number, then we found our integral part, else look for the same in the left or right side of mid depending upon the condition.
- After finding an integral part, we will find the fractional part.
- Initialize the increment variable by 0.1 and iteratively calculate the fractional part upto 5 decimal places.
- For each iteration, change increment to 1/10th of its previous value.
- Finally, return the answer computed.
Example: In this article, we will calculate the square root of the given Number using Binary Search.
Javascript
function findSqrt(number) {
let start = 0, end = number, mid, ans;
while (start <= end) {
mid = Math.floor((start + end) / 2);
if (mid * mid == number) {
ans = mid;
break ;
}
if (mid * mid < number) {
ans = start;
start = mid + 1;
}
else {
end = mid - 1;
}
}
let increment = 0.1;
for (let i = 0; i < 5; i++) {
while (ans * ans <= number) {
ans += increment;
}
ans = ans - increment;
increment = increment / 10;
}
return ans;
}
const n = 36;
console.log(findSqrt(n));
|
Share your thoughts in the comments
Please Login to comment...