求助大神,为何经常编写程序无法执行呢?比如判断素数。 int main(){int n; Cout<<"input number"<>n; For (int i=2;i<=sqrt(n);i++) {if((n%i)==0) break; else cout<<n<<"bushi sushu"<<endl;}}
Rerurn 0;
你确定你的代码没有写错?
#include <iostream>
using namespace std;
int main() {
int n;
bool flag = true;
cin >> n;
cout << "input number " << n << endl;
for (int i=2; i*i <= n; i++) {
if((n%i) == 0) { //表明不是素数
cout << n << "不是素数" << endl;
flag = false;
break;
}
}
if (flag)
cout << n << "是素数" << endl;
return 0;
}
你应该贴出是什么错误,如果你的程序确定没有错误,看看是不是项目建错了。。。
1.Cout<<"input number"<>n 这里应该是 << n 吧
2.当break 的时候,没有任何输出,看着应该是没反应吧
3.cmd 是不是闪退? 代码 return 前,加上试试 "system("pause");"
4、for、cout第一个字母不能大写
参考代码段:
https://github.com/707wk/Senior-middle-school/blob/master/Filling%20in%20the%20gaps.c