C++怎么用迭代法求根号a?
展开全部
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a,x,y;
cout << "please enter a number:"<<endl; //cin cout要分开写,cin是输入 cout是输出
cin>>a ;
y=a/2;
do {
x=y;
y=(x+a/x)/2;
} while (fabs(y-x)>1e-5); //少了括号
cout<<"根号a的值为:"<<y<<endl;
return 0;
}
#include <cmath>
using namespace std;
int main()
{
float a,x,y;
cout << "please enter a number:"<<endl; //cin cout要分开写,cin是输入 cout是输出
cin>>a ;
y=a/2;
do {
x=y;
y=(x+a/x)/2;
} while (fabs(y-x)>1e-5); //少了括号
cout<<"根号a的值为:"<<y<<endl;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询