#include <iostream>
using namespace std;

int findSqrtInt(int x) {
    int s = 0;
    int e = x;
    
    long long int mid = s + (e-s)/2;
    int ans = -1;
    while(s <= e){
        if(mid * mid == x){
            return mid;
        }else if(mid * mid < x){
            ans = mid;
            s = mid + 1;
        }else{
            e = mid - 1;
        }     
        mid = s + (e-s)/2;
    }
    return ans;
}

double findSqrtWithPrecision(int num, int sqrtInt, int precision){
    double factor = 1;
    double ans = sqrtInt;
    
    for(int i=0; i<precision; i++){
        factor = factor/10;
        for(double j=ans; j*j <= num; j+=factor){
            ans = j;
        }
    }
    return ans;
}

int main() {
    int n;
    cout << "Enter Number : " << endl;
    cin >> n ;
    cout << endl;
    
    int sqrtInt = findSqrtInt(n);
    int precision = 5;
    
    cout << "Square Root is : " << findSqrtWithPrecision(n, sqrtInt, precision) << endl;
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: