Program in c++ to find sum of all prime elements of array
//https://clanguage0.blogspot.com #include<stdio.h> #include<conio.h> int PrimeSum(int *a,int n) { int p=1,j,sum=0,i; for(i=0;i<n;i++) { for(p=1,j=2;j<=a[i]/2;j++) { if(a[i]%j==0) { p=0; break; } } if(p==1) sum+=a[i]; } return sum; } int main() { int a[100],n,i; printf("Enter sizee of array:-"); scanf("%d",&n); if(n>100) { printf("Invalid array size."); return 0; } printf("Enter %d numbers:-",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\nSum of the all prime elements of the given array =%d",PrimeSum(a,n)); getch(); }Output :-
0 Comments