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:
Home / array in c /
C Programs /
OldQuestionSoluton2019C /
sum of prime numbers in c
/ Enter n numbers in an array, and compute the sum of only prime numbers - C
- Blogger Comment
- Facebook Comment
Subscribe to:
Post Comments
(
Atom
)
0 comments:
Post a Comment