![](/img/trans.png)
[英]Getting an infinite loop in Babylonian Algorithm for square roots in C++
[英]using while loop for determining square roots of numbers c++
我正在运行一个c ++程序,使用while循环确定数字的平方根,在这里我定义了变量,尝试过但是没什么对我有用,任何手都可以帮到我。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n=10, N=0, i=0;
while(i<5)
N=i*n;
cout<<"numbers ="<<"\t Square root="<<sqrt(N)<<endl;
return 0;
}
程序成功执行,但不显示任何内容(emptyb行)。 谢谢。
while(i<5)
N=i*n;
因为您没有缩进或使用方括号,所以您可能错过了它,它等于:
while (i < 5) {
N = i * n;
}
因为该语言指定没有方括号,所以您仅将其应用于下一条语句并继续循环。
因此,这会永远循环,因为i
永远不会超过5岁才能退出循环。
只需在循环中添加花括号,然后检查要在哪里更改i和n。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.