 # Finding square root using newton raphson method

#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");
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;
}

#3

newton rapson method is used to find roots of given equation. it depends on formula
xn+1=xn-(f(xn))/(f(xn+1))
from equation, we can find square root. for eg:- suppose we want to find square root of 10.
i.e, let x = square root of 10.
on squaring we get, x2=10
x2-10=0
and then we find root of equation

// Function to find the root by newton raphson method

void Nwtnrpsn(double y)
{
double h = func(x) / derivFunc(x);
while (abs(h) >= EPSILON)
{
h = f(x)/derivf(x);
x = x - h;
}

``````cout << "The value of the root is : " << x;
``````

}

in above program, derivf(x) is derative of f(x)