c语言 迭代法求平方根

来源:百度知道 编辑:UC知道 时间:2024/06/03 20:41:54
#include <math.h>
#include <stdio.h>
void main( )
{ double x, y, y0 ;
printf( "输入一个正数:" ) ;
do scanf("%f", &x );
while( x<0 );
y = 1;
do
{ y0 = y;
y = 1/2*( y + x / y );
} while ( fabs( y - y0 ) / y > 0.00001);
printf("Square root of %f is %f\n", x, y );
}
这个程序到底哪错啦?各位大虾帮办忙呀!!

#include <math.h>
#include <stdio.h>
void main( )
{
double x, y, y0 ;
printf( "输入一个正数:" ) ;
do
{
scanf("%lf", &x );//格式lf
}
while( x<0 );
y = 1;
do
{
y0 = y;
y = 1.0/2*( y + x / y ); //1.0变浮点数
}
while ( fabs( y - y0 ) / y > 0.00001);
printf("Square root of %lf is %lf\n", x, y ); //格式lf
}

#include <stdio.h>
#include <math.h>
void main()
{
float x1,x2,a;
x2 = 1;
scanf("%f",&a);
x1=a/2;
while(fabs(x1-x2)>=1e-5)
{
x1=x2;
x2=0.5*(x1+a/x1);
}
printf("%f\n",x2);
}

#include <stdio.h>
#include <math.h>

int main(int argc, int *argv[]) {
float x=1,x0;
int n;
printf("please intput a number:");
scanf("%d",&n);
while