1

This is an example of finding square roots. I have to maintain this procedure to find the square root of any given number.

enter image description here

I have got a problem that, I have to find the square root of any given number without using the sqrt() function in C. And I have to follow this procedure that I have showed in this image. I have solved one. Here's my code:

int main(){

    double i,j,n;

    printf("Enter a number\n");

    scanf("%lf",&n);

    j=n;

    for(i=1;i<=n*2;i++)
    {
        j=(j+(n/j))/2;
    }

    printf("\nresult is %.4lf",j );

    return 0;
}

Is this code maintaining the procedure that is given in the image? If it is not, please how I should solve it? I am a beginner. If there's anything wrong, please tell me.

  • 4
    The code is using [Newton-Raphson](http://en.wikipedia.org/wiki/Newton's_method) (or 'divide and average') which works really well for square roots but is nothing to do with the diagram. – Jonathan Leffler Apr 25 '14 at 21:21
  • have you tried google `square root algorithm c` ? – AndersK Apr 25 '14 at 21:34
  • I have searched google .i have found this which is shown in my picture.here the link `http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Decimal_.28base_10.29` @ Claptrap. In image which algorithm followed. – user3531769 Apr 25 '14 at 22:11
  • "Is this code maintaining the procedure that is given in the image?" No, the 2 methods are different. 1st method works nice with integers. Are you certain the input is floating point and not an integer? – chux - Reinstate Monica Apr 25 '14 at 22:15
  • Please anyone tell me the fomula/mathod/algorithm name that is used in image.or give me solution. http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Examples this is another link.I need to solve by this procedure/method – user3531769 Apr 25 '14 at 22:19

0 Answers0