1027.c

来自「用于学习的C原程序」· C语言 代码 · 共 53 行

C
53
字号

/*********************************************************************
 * a demo program which to find all the prime numbers from an array  *
 * Input : N, and N numbers a[100]                                   *
 * output: all the prime number and the sum of them                  *
 *                                                                   *
 * Date: 20th April, 2007                                            *
 *       by ZQ                                                       *
 *********************************************************************/
 
#include <stdio.h>
int main()
{
    //declare N and an array a[100] for N numbers of integer 
    int N,a[100],b[100];
    //declare the result variable
    int s;
    //declare the loop variables
    int i,j,k;
    
    /* input N and N numbers of integer from keyboard */
    
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%d",&a[i]);
    }
    
    /* find the prime number and add them to s*/
    
    s=0;
    for(i=0;i<N;i++)
    {
        for(j=2;j<=sqrt(a[i]);j++)
        {
            if(a[i]%j==0) // it means a[i] has a fact of j, it is not a prime number */
                break;
        }//end of loop j
        
        // it means end from the for-loop, not break from the for-loop  
        if(j>sqrt(a[i]))
        {
            s+=a[i];
            b[k]=a[i];
            printf("%d ",b[k]);
            k++;
         }//end of if(...)
    }// end of loop i
    
    /* output the result */
    
    printf("\ns=%d\n",s);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?