永发信息网

想知道用C++怎么用迭代法求根号a,a是输入的

答案:3  悬赏:40  手机版
解决时间 2021-03-18 13:34
  • 提问者网友:玫瑰园
  • 2021-03-17 21:19
#include <iostream>
#include <cmath>
using namespace std;
void main()
{
float a,x,y;
cin>>"please enter a number:">>a>>endl;
y=a/2;
do {
x=y;
y=(x+a/x)/2;
}
while fabs(y-x)>1e-5;
cout<<"根号a的值为:"<<y<<endl;
}
这个是怎么错的啊?
最佳答案
  • 五星知识达人网友:夜风逐马
  • 2021-03-17 22:19
#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;
}
全部回答
  • 1楼网友:人類模型
  • 2021-03-18 00:27
#include <iostream> #include <cmath> using namespace std; void main() { float a,x,y; cout<<"please enter a number:";//你居然把这个放到了cin的后面,然后还加了个endl,这些都是输出才可以用的 cin>>a; y=a/2; do { x=y; y=(x+a/x)/2; } while (fabs(y-x)>1e-5);//while后面的条件用括号括起来 cout<<"根号a的值为:"<<y<<endl; }
  • 2楼网友:轮獄道
  • 2021-03-17 23:36
#include &lt;iostream&gt; #include &lt;cmath&gt; using namespace std; int main() { float a,x,y; cout &lt;&lt; "please enter a number:"&lt;&lt;endl; //cin cout要分开写,cin是输入 cout是输出 cin&gt;&gt;a ; y=a/2; do { x=y; y=(x+a/x)/2; } while (fabs(y-x)&gt;1e-5); //少了括号 cout&lt;&lt;"根号a的值为:"&lt;&lt;y&lt;&lt;endl; return 0; }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯