C经典算法2、素数问题

/*题目2:判断101-200之间有多少个素数,并输出所有素数。
程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整
除,则表明此数不是素数,反之是素数。*/
/*20190217:14:40*/
/*
101 103 107 109 113 121 127 131 137 139
149 151 157 163 167 169 173 179 181 191
193 197 199
 The total is 23
--------------------------------
Process exited after 0.1534 seconds with return value 0
请按任意键继续. . .
*/

#include<stdio.h>
#include<math.h>
int main()
{
    int i,j,k,h=0,mask=1;
    for(i=100;i<201;i++)
    {
        k = sqrt(i+1);
        //h = i/2;
        for(j=2;j<k;j++)
        if(i%j==0)
        {
          mask = 0;    
          break;
        }
        if(mask)
        {
            printf("%-4d",i);
            h++;
        
        if(h%10==0)
        printf("\n");
        }
        mask =1;
    }
    printf("\n The total is %d",h);
 } 

猜你喜欢

转载自blog.csdn.net/qq_40025335/article/details/87708929