Hello I wrote this program to find the square root of any number that the user enters using newtons method but it is not working. The initial guess is the number entered by the user divided by 10. And after running through equation and calculating the new value it compares it with the old value(xm1) and if the difference between them is less then 0.001 then it prints out the value it calculated. I would be very greatful if someone can corrent my mistake or point out to me where I made the mistake.
Code:
#include <stdio.h>
#include <math.h>
#define SIZE 5
int main()
{
float x, number,xm1,r;
float difference;
int a;
printf("Please enter the number of which you wish the square root of: ");
scanf("%f",&number);
a=number/2;
xm1=0;
difference = 1;
x = a - (pow(a,2)-number)/(2*a);
xm1=x;
while(difference >= 0.01)
{
r= xm1- (pow(xm1,2)-number)/(2*xm1);
difference = r-xm1;
xm1=r;
}
if(difference <= 0.01)
{
printf("the square root is %f\n",r);
}
return 0;
}