电视剧悬崖录音剪辑:c++的程序语言设计(是关于质数的)

来源:百度文库 编辑:高校问答 时间:2024/04/29 08:29:22
用穷举法找出1-100的质数并显示,要使用while,do-while,for循环的语句实现.
谢谢了
那你就教教我呢,我可以追加分数的

#include "iostream.h"
void main()
{
int i,j,index;
for (i=2;i<=100;i++)
{
index=1;
for (j=2;j<i/2;j++)
{
if(i % j==0) index=0;
}
if (index==1) cout<<i<<'\n';
}

}

#include <iostream.h>
#include <math.h>
void main()
{

cout<<"2是质数;"<<endl;
for(int i=3;i<=100;i++)
for(int j=2;j<=(int)sqrt(i);j++)
{
if((i%j)!=0)
{
if(j!=(int)sqrt(i))
continue;
else cout<<i<<"是质数;"<<endl;
}

else break;
}

}

这个太初级了