/* Solution of an algebraic equation x*x-n=0 by Newton-Raphson method */
#include<stdio.h>
#include<math.h>
int main()
{
int count=0;
float x0, xn, fx, dfx, n;
printf("Enter the number whose sq. root is required:");
scanf("%f", &n);
printf("Initial guess value x0:");
scanf("%f", &x0);
/* Iteraion begins */
label:
fx=x0*x0-n;
dfx=2*x0;
xn=x0-fx/dfx;
if(fabs(xn-x0)<0.0001)
printf("The square root of %f is:%f", n, xn);
else
{
x0=xn;
count=count+1;
if(count>100)
printf("Solution does not converge");
else
goto label;
}
return(0);
}
#include<stdio.h>
#include<math.h>
int main()
{
int count=0;
float x0, xn, fx, dfx, n;
printf("Enter the number whose sq. root is required:");
scanf("%f", &n);
printf("Initial guess value x0:");
scanf("%f", &x0);
/* Iteraion begins */
label:
fx=x0*x0-n;
dfx=2*x0;
xn=x0-fx/dfx;
if(fabs(xn-x0)<0.0001)
printf("The square root of %f is:%f", n, xn);
else
{
x0=xn;
count=count+1;
if(count>100)
printf("Solution does not converge");
else
goto label;
}
return(0);
}
Comments
Post a Comment