• Register
2 votes
994 views

Problem :

I have few years of experience in working with C programming. Now I am preparing for the interview and while preparing for it I came to below question for which I don’t have any working code.

C program to find square root of a number without using sqrt function”.

Can somebody help me in answering above question?

8 5 2
3,230 points

Please log in or register to answer this question.

1 Answer

2 votes

Solution :

This is very tricky question for any programmer. I have also asked this question in many interviews but most of the time I got wrong answers from the candidates. So I am trying to answer it for you.

Below is the code which needs only 6 iterations to meet to the maximum precision for the double numbers:

#include <math.h>
double SquareRoot(double x) {
    if (x <= 0)
        return 0;       // if in case the negative number throws the exception
    int exp = 0;
    x = frexp(x, &exp); // extract the binary exponent from the x
    if (exp & 1) {      // to get the exponent to be even
        exp--;
        x *= 2;
    }
    double y = (1+x)/2; // this is our first approximation
    double z = 0;
    while (y != z) {    // we are compare doubles
        z = y;
        y = (y + x/y) / 2;
    }
    return ldexp(y, exp/2); //we can have  multiply answer by 2^(exp/2)
}
8 4 1
5,680 points

Related questions

0 votes
1 answer 12 views
12 views
Problem: I need someone to understand this problem and find out the solution. C program to find square root of a number without sqrt
asked Apr 26 Ifra 34.7k points
0 votes
1 answer 110 views
1 vote
1 answer 81 views
81 views
Problem: I want to find a square root value of an integer number in a C++ program. Well, the condition is that, I can&rsquo;t use the sqrt() function in my program. Let say, the given number is 16, and output should produce 4. If the given number is not a perfect ... Is it possible to do so as per my requirement? If possible, please help me to find the way. I look forward to your answers. Thanks.
asked May 3, 2020 Gavin 15.3k points
0 votes
1 answer 9 views
9 views
Problem: I used Google to try to find a solution on the web for the above question. But I got no useful hints how to Find the square root of a number in c without using sqrt. Can I get Pointer information about my problem with this?
asked Mar 30 zayed1 58.7k points
0 votes
1 answer 6 views
6 views
Problem: I am stuck with this ... Please help me that how to deal with this? How to write square root in python?
asked Apr 4 Ifra 34.7k points
1 vote
1 answer 22 views
22 views
Problem: Hello Kodlogs, I am a college student. I recently started learning computer programming using the Python programming language. I was solving some mathematical operations through Python programs. I&rsquo;ve stuck to solve a problem regarding square-root. In my program, I am ... an integer number? If you share the tip with me it would really help me a lot. Thanks for your kind concern.
asked Jul 11, 2020 adamSw 11.3k points
0 votes
1 answer 1.2K views
0 votes
1 answer 5 views
5 views
Problem: How to fix this: square of a number in python
asked Mar 15 Wafa Abu Yousef 6.1k points
2 votes
1 answer 35 views
35 views
Problem: Hello sir, I was studying the python language and I started implementing the program I can do it by the simple multiplying by itself but this looks l very simple is there is another way in which we can achieve this..? Below is my simple solution. x = input("Enter the number") num = x * x print("Square number is",num) I hope another way to solve the problem will get as soon as possible.
asked Jun 3, 2020 Gavin 15.3k points
0 votes
0 answers 3 views
3 views
Problem: I need someone&rsquo;s help to fix it .. How to find inverse of 4x4 matrix example
asked 5 hours ago Ifra 34.7k points