c语言迭代法求平方根,x=√a,迭代公式为Xn-1=1/2(Xn+a/Xn)
这是我的编程,但运行总得为0,求指导#include<stdio.h>#include<math.h>intmain(){doublem,n,a;printf("ente...
这是我的编程,但运行总得为0,求指导
#include<stdio.h>
#include<math.h>
int main()
{
double m,n,a;
printf("enter a positive number:");
scanf("%d",&a);
m=1;
n=(m+a/m)/2;
while(fabs(n-m)>=1e-5)
{
m=n;
n=(n+a/n)/2;
}
printf("The square root of %5.2f is %8.5f\n",a,n);
return 0;
} 展开
#include<stdio.h>
#include<math.h>
int main()
{
double m,n,a;
printf("enter a positive number:");
scanf("%d",&a);
m=1;
n=(m+a/m)/2;
while(fabs(n-m)>=1e-5)
{
m=n;
n=(n+a/n)/2;
}
printf("The square root of %5.2f is %8.5f\n",a,n);
return 0;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询