What are diffenrences between "do while" and "while" ?
#include <stdio.h>#include <math.h>
int main()
{
float a,x0,x1;
printf("enter a positive number:");
scanf("%f",&a);
x0=a/2;
x1=(x0+a/x0)/2;
do
{x0=x1;
x1=(x0+a/x0)/2;
}while(fabs(x0-x1)>=1e-5);
printf("The square root of %5.2f is %8.5f\n",a,x1);
return 0;
}
这个程序是用do while编写,我想改成while循环,为什么下边的不对呢?求教大神?
#include<stdio.h>
#include<math.h>
int main()
{double a,x1,x0;
printf("please enter a:");
scanf("%d",&a);
x0=a/2;
x1=(x0+a/x0)/2;
while(fabs(x1-x0)<1e-5)
{
x1=0.5*(x0+a/x0);
x0=x1;
}
printf("%8.5f",x1);
return 0;
}