c语言编程练习题:7-33 统计素数并求和-LMLPHP

#include <stdio.h>

int is_prime(int n){
    int i;
    int r=1;
    if (n==1){r=0;}
    if (n>1){
        for (i=2;i<n;i++){
            if (n%i==0){r=0;break;}
    }
    }
    
    return r;
}

int main(){
    int M;
    int N;
    int i;
    int count=0;
    int my_sum=0;
    if (scanf("%d %d",&M,&N)!=0 && M>=1 && N<=500 && M<=N){
        for (i=M;i<=N;i++){
            if (is_prime(i)){count++;my_sum+=i;}
        
        }
        printf("%d %d",count,my_sum);
    }else{
        printf("输入数据错误");
    }
    return 0;
}

代码来自:https://yunjinqi.top/article/158

05-29 16:06