There is a better algorithm, which needs at most 6 iterations to converge to maximum precision for double numbers:

#include <math.h>

double sqrt(double x) {
    if (x <= 0)
        return 0;       // if negative number throw an exception?
    int exp = 0;
    x = frexp(x, &exp); // extract binary exponent from x
    if (exp & 1) {      // we want exponent to be even
        exp--;
        x *= 2;
    }
    double y = (1+x)/2; // first approximation
    double z = 0;
    while (y != z) {    // yes, we CAN compare doubles here!
        z = y;
        y = (y + x/y) / 2;
    }
    return ldexp(y, exp/2); // multiply answer by 2^(exp/2)
}

Algorithm starts with 1 as first approximation for square root value. Then, on each step, it improves next approximation by taking average between current value y and x/y. If y = sqrt(x), it will be the same. If y > sqrt(x), then x/y < sqrt(x) by about the same amount. In other words, it will converge very fast.

UPDATE: To speed up convergence on very large or very small numbers, changed sqrt() function to extract binary exponent and compute square root from number in [1, 4) range. It now needs frexp() from <math.h> to get binary exponent, but it is possible to get this exponent by extracting bits from IEEE-754 number format without using frexp().

Answer from mvp on Stack Overflow
Top answer
1 of 9
41

There is a better algorithm, which needs at most 6 iterations to converge to maximum precision for double numbers:

#include <math.h>

double sqrt(double x) {
    if (x <= 0)
        return 0;       // if negative number throw an exception?
    int exp = 0;
    x = frexp(x, &exp); // extract binary exponent from x
    if (exp & 1) {      // we want exponent to be even
        exp--;
        x *= 2;
    }
    double y = (1+x)/2; // first approximation
    double z = 0;
    while (y != z) {    // yes, we CAN compare doubles here!
        z = y;
        y = (y + x/y) / 2;
    }
    return ldexp(y, exp/2); // multiply answer by 2^(exp/2)
}

Algorithm starts with 1 as first approximation for square root value. Then, on each step, it improves next approximation by taking average between current value y and x/y. If y = sqrt(x), it will be the same. If y > sqrt(x), then x/y < sqrt(x) by about the same amount. In other words, it will converge very fast.

UPDATE: To speed up convergence on very large or very small numbers, changed sqrt() function to extract binary exponent and compute square root from number in [1, 4) range. It now needs frexp() from <math.h> to get binary exponent, but it is possible to get this exponent by extracting bits from IEEE-754 number format without using frexp().

2 of 9
16

Why not try to use the Babylonian method for finding a square root.

Here is my code for it:

double sqrt(double number)
{
    double error = 0.00001; //define the precision of your result
    double s = number;

    while ((s - number / s) > error) //loop until precision satisfied 
    {
        s = (s + number / s) / 2;
    }
    return s;
}

Good luck!

🌐
GeeksforGeeks
geeksforgeeks.org › dsa › square-root-of-a-number-without-using-sqrt-function
Square root of a number without using sqrt() function - GeeksforGeeks
March 30, 2023 - // C# Program to find square // root of a number using System; using System.Collections.Generic; public class GFG { public static double findSqrt(double x) { // for 0 and 1, the square roots are themselves if (x < 2) return x; // considering the equation values double y = x; double z = (y + (x / y)) / 2; // as we want to get upto 5 decimal digits, the // absolute difference should not exceed 0.00001 while (Math.Abs(y - z) >= 0.00001) { y = z; z = (y + (x / y)) / 2; } return z; } static public void Main() { double n = 3; double ans = findSqrt(n); ans = Math.Round(ans, 5); Console.WriteLine(ans
🌐
Sololearn
sololearn.com › en › Discuss › 2228331 › how-can-we-calculate-square-root-without-using-any-builtin-function-in-python
How can we calculate square root without using any built-in function in python? | Sololearn: Learn to code for FREE!
Try this new method without using sqrt() and pow() num = float(input("Enter num: ")) a = 1.0 while(1): b = a*a if num-0.1 < b < num+0.1 : break else: pass a = a + 0.01 print("\n Square root value is: ",round(a,2)) Hope its helpful ...
🌐
Quora
quora.com › How-do-I-write-a-program-for-finding-the-square-root-of-a-number-without-using-the-sqrt-function
How to write a program for finding the square root of a number without using the sqrt function - Quora
Answer (1 of 27): #include void main() { int n; //user will enter d no. float temp,sqt; printf(“Enter d no.”); scanf(“%d”,&n); sqt=n/2; temp=0; while(sqt!=temp) { temp=sqt; sqt=(n/temp+temp)/2; } printf(“the square root of %d is%f”,n,sqt); }
🌐
Reddit
reddit.com › r/learnprogramming › best way to calculate the square root without using a sqrt() function? (in a tech interview)
r/learnprogramming on Reddit: Best way to calculate the square root without using a sqrt() function? (in a tech interview)
June 11, 2015 -

As far as I know of there are two methods to calculate the square root: newtons method and a modified binary search,. Which one would you say is better for a technical interview? I know that the binary search method is O(LogN) but what is the time complexity of newtons method? Is newtons method faster?

🌐
LeetCode
leetcode.com › problems › sqrtx
Sqrt(x) - LeetCode
Can you solve this real interview question? Sqrt(x) - Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.
🌐
Edureka Community
edureka.co › home › community › categories › c++ › finding square root without using sqrt function
Finding square root without using sqrt function | Edureka Community
July 13, 2022 - I tried to incorporate the technique I had discovered for calculating the square root without utilising ... no problem. Please offer a solution.
Find elsewhere
🌐
Reddit
reddit.com › r/learnprogramming › sqrt without calculator
r/learnprogramming on Reddit: sqrt without calculator
June 15, 2021 -

In python, I am tasked with finding the floor square root of a number without using any built-in operators or functions. I have no idea how to start this, since using a for loop can only check the integer roots.

ex.

in : 8
out : 2
exp: sqrt(8) = 2.82842..., but floor(2.82842..) = 2
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › floor-square-root-without-using-sqrt-function-recursive
Floor square root without using sqrt() function : Recursive - GeeksforGeeks
July 12, 2025 - Base Case: The recursive call will get executed till square of mid is less than or equal to N and the square of (mid+1) is greater than equal to N. ... If the base case is not satisfied, then the range will get changed accordingly. If the square of mid is less than equal to N, then the range gets updated to [mid + 1, end] ... // C++ implementation to find the // square root of the number N // without using sqrt() function #include <bits/stdc++.h> using namespace std; // Function to find the square // root of the number N using BS int sqrtSearch(int low, int high, int N) { // If the range is st
🌐
Medium
medium.com › @Neelesh-Janga › q-69-leetcode-square-root-calculator-without-math-library-in-java-4797808be499
Find Square Root Without Math Library in Java | Neelesh | Medium
December 16, 2023 - Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well. You must not use any built-in exponent function or operator.
🌐
Tutorial Gateway
tutorialgateway.org › java-program-to-find-square-root-of-a-number-without-sqrt
Java Program to find Square Root of a Number without sqrt
December 20, 2024 - In this example, we used the binary search to find the square root. package RemainingSimplePrograms; import java.util.Scanner; public class SqrRootOfNum3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Please Enter Any Number = "); int num = sc.nextInt(); int squareroot = squareRootWithoutsqrt(num); System.out.println("\nThe Square Root of a Number without sqrt = " + squareroot); } public static int squareRootWithoutsqrt(int num) { if(num < 2) { return num; } int sqrtVal = 1; while(sqrtVal * sqrtVal <= num) { sqrtVal++; } return sqrtVal - 1; } }
🌐
Our Code World
ourcodeworld.com › articles › read › 884 › how-to-get-the-square-root-of-a-number-without-using-the-sqrt-function-in-c
How to get the square root of a number without using the sqrt function in C | Our Code World
March 30, 2019 - #include<stdio.h> void main() { int number; float temp, sqrt; printf("Provide the number: \n"); scanf("%d", &number); // store the half of the given number e.g from 256 => 128 sqrt = number / 2; temp = 0; // Iterate until sqrt is different of temp, that is updated on the loop while(sqrt != temp){ // initially 0, is updated with the initial value of 128 // (on second iteration = 65) // and so on temp = sqrt; // Then, replace values (256 / 128 + 128 ) / 2 = 65 // (on second iteration 34.46923076923077) // and so on sqrt = ( number/temp + temp) / 2; } printf("The square root of '%d' is '%f'", number, sqrt); } The code works like this: initially, the program will prompt the user for the number from which we want to find the square root.
🌐
TutorialsPoint
tutorialspoint.com › how-to-perform-square-root-without-using-math-module-in-python
How to perform square root without using math module in Python?
Without using the math module, the simplest approach to find the square root of a number in Python is to use the built-in exponential operator ** (It is an exponent operator because it calculates the power of the first operand to the power of the second operand).
🌐
Cplusplus
cplusplus.com › forum › beginner › 149040
Finding square root without using sqrt f - C++ Forum
Then you just need to translate that by hand algorithm into C++. Figure out how accurate you want to be, e.g. is within .001 of the actual square good enough? You can create your own function "mySqrt" (or whatever you want to call it) and call that instead of the one in the <cmath> header.
🌐
TakoVibe
takovibe.com › home › blog › finding square root without using sqrt function in python
Finding Square Root Without Using sqrt function in Python | Rahul Beniwal | TakoVibe
December 16, 2025 - Learn how to calculate square roots in Python without using the built-in sqrt() function. Explore binary search and Newton’s method, two fascinating approaches inspired by computer science and math.