

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding square root of a number without using Math.sqrt() in JavaScript
We are required to write a JavaScript function that takes in a positive integer as the only argument. The function should find and return the square root of the number provided as the input.
Example
Following is the code −
const squareRoot = (num, precision = 0) => { if (num <= 0) { return 0; }; let res = 1; const deviation = 1 / (10 ** precision); while (Math.abs(num - (res ** 2)) > deviation) { res -= ((res ** 2) - num) / (2 * res); }; return Math.round(res * (10 ** precision)) / (10 ** precision); }; console.log(squareRoot(16)); console.log(squareRoot(161, 3)); console.log(squareRoot(1611, 4));
Output
Following is the output on console −
4 12.689 40.1373
- Related Questions & Answers
- Finding square root of a number without using library functions - JavaScript
- Finding square root of a non-negative number without using Math.sqrt() JavaScript
- How to perform square root without using math module in Python?
- Check if a number is perfect square without finding square root in C++
- Square root function without using Math.sqrt() in JavaScript
- Check for perfect square without using Math libraries - JavaScript
- Get minimum number without a Math function JavaScript
- Get square root of a number using Math.sqrt in Java
- 8086 program to find the square root of a perfect square root number
- How to get the square root of a number in JavaScript?
- Program to check number is perfect square or not without sqrt function in Python
- 8085 program to find square root of a number
- 8086 program to find Square Root of a number
- Returning a range or a number that specifies the square root of a number in JavaScript
- How to calculate square root of a number in Python?
Advertisements