设为首页 加入收藏

TOP

c语言和c++:2种方法:打印100~200 之间的素数
2015-11-19 23:07:20 来源: 作者: 【 】 浏览:7
Tags:言和 方法 打印 100 200 之间 素数
方法1用c语言 编程
#include 
#include 
int main()
{
int i=0;
    int count=0;
for(i=101;i<=199;i+=2)
{
int j=0;
for(j=3;j<=0.5*sqrt(i);j+=2)
{
if(i%j==0)
{
break;
}
}
if(j>0.5*sqrt(i))
{
count++;
printf("%d  ",i);
}
}
printf("count=%d\n",count);
return 0;
}

?

输出结果:
101 ?103 ?107 ?109 ?113 ?119 ?121 ?127 ?131 ?133 ?137 ?139 ?143 ?149 ?151 ?157 ?161 ?163 ?167 ?169 ?173 ?179 ?181 ?187 ?191 ?193 ?197 ?199 ?count=28
Press any key to continue
用c++编程
#include
#include
using namespace std;
int main()
{
int i=0;
? ? int count=0;
for(i=101;i<=199;i+=2)
{
int j=0;
for(j=3;j<=0.5*sqrt(i);j+=2)
{
if(i%j==0)
{
break;
}
}
if(j>0.5*sqrt(i))
{
count++;
cout<
}
}
cout<
return 0;
}
输出结果:
101 103 107 109 113 119 121 127 131 133 137 139 143 149 151 157 161 163 167 169 173 179 181 187 191 193 197 199 28
Press any key to continue
方法2用 c语言编程
#include
#include
main()
{   
    int i,k,tag,j=0;
    for (i=100;i<=200;i++)
{tag=0;
      for(k=2;k 
  

?

输出结果:
101 103 107 109 113
127 131 137 139 149
151 157 163 167 173
179 181 191 193 197
199 Press any key to continue
?
?
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇[C语言][面试题]使用main函数的参.. 下一篇c语言:输出乘法口诀表

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: