Program to find square root of number


#1

write a program to print the square root value of a number using newton raphson method


#2

#include <stdio.h>
#define DIF 0.0001

float absolute (float);
float test (float, float);

int main (void)
{

int i;
float num, x, y;

    printf("This program will determine the square root using the Newton-Raphson Method.\n");
    printf("Please enter a number: ");
    scanf("%f", &num);

      x = num / 2;

            if (num <= 0)
            {
                    if (num == 0)
                            printf("The square root of 0 is 0.");
                    else
                            printf("Sorry that number is negative.");
            }
            else
            {
              i = 0;

                    printf("Find the square root of %f\n", num);

                    do
                    {
                            y = x - (x*x - num)/(2*x);
                            printf("%i: x = %f \n", i, y);
                            i++;
                    }
                    while (absolute(test(num , y)) > DIF);

return 0;

}

float absolute (float x)
{
if(x < 0)
x = -x;
return x;
}

float test (float num, float y)
{
x = num - y;
return x;
}