Enter n numbers in an array, and compute the sum of only prime numbers - C

Q. Write a program to enter n numbers in an array, and compute the sum of only prime numbers in the array.   BIT Ist Sem - 2019, Group B, Q.No. 4 - 8 Marks

==> Code goes like this:

#include<stdio.h>
#define SIZE 5
int main()
{
int a[SIZE],i,j,flag,n,sum=0;
printf("\n Enter %d elements:",SIZE);
for(i=0;i<SIZE;i++)
scanf("%d",&a[i]); 
//Checking prime number
for(i=0;i<SIZE;i++)
{
    n=a[i];
    if(n!=1){
   
    flag=0;
    for(j=2;j<n &&n!=1;j++)
        {
        if(n%j==0)
            {
            flag=1;
            break;
            }
        }
    if(flag==0){
    printf("%d is prime \n",n);
    sum=sum+n;   //summing up prime numbers
    }
}
}
printf("\n The sum of prime numbers is %d :",sum);
return 0;
}

Output:


Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment