Untitled
unknown
plain_text
a year ago
1.3 kB
10
Indexable
#include <stdio.h>
#include <stdlib.h>
sumofprime(int *ptr,int n)
{
int sum=0,i,j;
for (i=0;i<n;i++)
{
int l=ptr[i],ip=0;
if (l==1)
{
ip=3;
}
else if (l==2)
{
sum+=l;
}
else
{
for (j=2;j<l;j++)
{
if (l%j==0)
{
ip=1;
break;
}
}
if (ip==0)
{
sum+=ptr[i];
}
}
}
return sum ;
}
int main ()
{
int i,n,*ptr,sum;
printf("enter the number of elements of the array: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
if (ptr == NULL)
{
printf("Memory is full ");
}
else
{
printf("enter the values of the array:\n");
for (i=0;i<n;i++)
{
printf("enter the %d element ",i+1);
scanf("%d",&ptr[i]);
}
printf("the elements of the array are : \n");
for (i=0;i<n;i++)
{
printf("%d \n",ptr[i]);
}
sum=sumofprime(ptr,n);
printf("the sum of prime numbers in th array is: %d",sum);
}
free(ptr);
return 0;
}Editor is loading...
Leave a Comment