一道C++编程题,初学C++,变成出现问题,望高手指点。求100~200的素数。下面的编程存在我不知道的问题。

请帮忙改正下面的程序,谢谢!
#include "stdafx.h"
#include <iostream>
#include "math.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int a;
a>100 && a<200;
if (a%2==0 || a%3==0 || a%5==0 || a%7==0 || a%11==0 || a%13==0 || a%17==0)
cout<<' '<<endl;
else cout<<a<<endl;
return 0;
}

基本没啥问题,只是楼主对C++一些语法还不是很熟练。楼主的方程中 a>100 && a<200 这一句,C++是完全无法识别的。估计楼主是想给a定一个范围吧,但是C++里面不是这么定义的,这种时候应该建立循环,从100开始,循环到200,逐个判断。循环可以使用for循环和while循环,我两种都写一下吧。
下面给出我修改后的答案。

#include "stdafx.h"
#include <iostream>
#include "math.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
for(int a=100;a<=200;a++){
if (a%2==0 || a%3==0 || a%5==0 || a%7==0 || a%11==0 || a%13==0 || a%17==0)
cout<<' '<<endl;
else
cout<<a<<endl;
}
return 0;
}

#include "stdafx.h"
#include <iostream>
#include "math.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int a=100;
while(a!=200){
if (a%2==0 || a%3==0 || a%5==0 || a%7==0 || a%11==0 || a%13==0 || a%17==0)
cout<<' '<<endl;
else
cout<<a<<endl;
a++;
}
return 0;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-03-15
改成int main应该就行了。
对了,你的if语句套个花括号,层次会好些
cout和endl前面加个std::
第2个回答  2011-03-15
for(a = 100,a<=200;a++)
相似回答
大家正在搜