C语言复习---用筛选法求100之内的素数

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

int main()
{
    int i, j;
    int a[101] = { 0 };
    for (i = 2; i <= 100;i++)
    {
        for (j = 2; j <= sqrt(i);j++)
            if (i%j==0)
                break;
        if (j >= sqrt(i))
            a[i] = 1;
    }

    for (i = 1; i <= 100; i++)
        if (a[i])
            printf("%d ", i);
    system("pause");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ssyfj/p/9389330.html
今日推荐