C语言-求100以内的全部素数

2020-01-07  本文已影响0人  广陵周惊蛰

问题描述:求100以内的全部素数

源代码:

/*求100以内的全部素数*/

#include <stdio.h>
#include <math.h>

int main(void)
{
    int count,i,m,n;
    count=0;
    for(m=2;m<=100;m++){
        n=sqrt(m);
        for(i=2;i<=n;i++)
            if(m%i==0)  
            break;
        if(i>n){
            printf("%6d",m);
            count++;
            if(count%10==0)
                printf("\n");
        } 
    } 
    printf("\n");
    
    return 0;
 } 

运行结果:

100以内素数

注意事项:

sqrt() --> 取根号

程序参数:

上一篇 下一篇

猜你喜欢

热点阅读