[C]统计[1,100]之间的素数个数,并求和

 

//统计[1,100]之间的素数个数,并求和
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
bool isPrime(int );
int main()
{
    int num;
    // scanf("%d", &num);
    int sum, i;
    sum = i = 0;
    for (num = 1; num <= 100; ++num)
    {
        if (isPrime(num))
        {
            i++;
            sum += num;
        }
    }
    printf("there are %d prime between 1 and 100; and the sum is %d\n", i, sum);
}

bool isPrime(int num)
{
    int n = (int) sqrt(num);
    int i;
    for (i = 2; i <= n; ++i)
    {
        if (num % i == 0)
            break;
    }
    if (i > n && num != 1)
        return true;
    else
        return false;
}

 

posted @ 2020-06-14 19:23  profesor  阅读(853)  评论(0编辑  收藏  举报