Find square root of number upto given precision using binary search in C++



In this article, we will learn how to find the square root of a number up to a given precision by using binary search algorithm and implement it in C++. Before dive into the concept, make sure that you have a basic understanding of binary search algorithm.

Square Root of a Number Upto a Given Precision

In this problem, you are given a positive floating point number N and a positive integer P. The task is to find the square root of N up to P decimal places using binary search.

Scenario 1

Input: N = 25, P = 3

Output: 5.000

Explanation: The square root of 25 is 5, and when formatted to 3 decimal places, it becomes 5.000.

Scenario 2

Input: N = 2.59, P = 2

Output: 1.61

Explanation: The square root of 2.59 is approximately 1.609347..., and when formatted to 2 decimal places, it becomes 1.61. Check decimal rounding rules for more details.

Binary Search Algorithm to Find Square Root

To find the square root of a number N up to P decimal places using binary search, we can follow these steps:

  • Input the number N and the precision P.
  • Convert the integer precision P to a floating point number by, floatP = 10^(-P).
  • Initialize two float variables, low = 0 and high = N. If N is less than 1, set high to 1.
  • Start a while loop with condition (high - low) > floatP.
  • Inside the loop calculate the mid-point as mid = (low + high) / 2.
  • Calculate the square of mid as midSquare = mid * mid. If midSquare is equal to N, return mid.
  • If midSquare is less than N, set low = mid, otherwise set high = mid.
  • After the loop ends, return the value of low formatted up to P decimal places.

C++ Program to Find Square Root Using Binary Search

Here is the C++ code that implements the above algorithm. You can change the values of N and P to test with different inputs.

#include <iostream>
#include <iomanip>
#include <cmath> // Required for pow()
using namespace std;

double squareRoot(double N, double floatP) {
    double low = 0, high = N;

    // For numbers between 0 and 1, set high to 1
    if (N < 1) high = 1;

    while ((high - low) > floatP) {
        double mid = (low + high) / 2;
        if (mid * mid < N)
            low = mid;
        else
            high = mid;
    }

    return low;
}

int main() {
    double N = 38, floatP;
    int P = 4;

    cout << "Number: " << N << endl;
    cout << "Precision (number of decimal places): " << P << endl;

    // Convert to actual decimal precision
    floatP = pow(10, -1 * P);

    // Call binary search function to find square root
    double result = squareRoot(N, floatP);

    // Set output precision accordingly
    cout << fixed << setprecision(P);
    cout << "Square root of " << N << " is approximately " << result << endl;
    return 0;
}

The output of the above code will be:

Number: 38
Precision (number of decimal places): 4
Square root of 38.0000 is approximately 6.1644

Time and Space Complexity

The time complexity of the binary search algorithm used to find the square root is O(log(N * 10^P)), where N is the input number and P is the number of decimal places. This is because we are effectively performing a binary search on the range [0, N] with a precision of 10^(-P).

The space complexity is O(1) since we are not using any arrays or other data structures to store intermediate results, just a few variables for calculations.

Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-08-04T19:02:57+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements