码迷,mamicode.com
首页 > 其他好文 > 详细

c - 输出 101 至 200之间的素数.

时间:2014-11-15 01:22:20      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   sp   for   div   log   bs   

 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 //判断 101-200 之间有多少个素数,并输出所有素数.
 5 int
 6 main(void) {
 7     int s = 101, e = 200;
 8     int count = 0;    //素数总个数.
 9     int i;
10     int sq;    //对每个数开方.
11     
12     for(i = s; i <= e; i++)    {
13         int isPrime = 1;    //标识当前数是否为素数,素数为1,0为非素数.
14         sq = (int)sqrt((float)i);
15         for(int j = 2; j <= sq; j++) {
16             if(i%j == 0) {    //非素数.
17                 isPrime = 0;
18                 break;;
19             }
20         }
21         
22         if(isPrime) {
23             printf("%d\n", i);
24             ++count;
25         }
26     }
27     
28     printf("素数总个数为:%d\n", count);
29 }

 

c - 输出 101 至 200之间的素数.

标签:style   blog   io   color   sp   for   div   log   bs   

原文地址:http://www.cnblogs.com/listened/p/4098612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!