In: Computer Science
Write program to store ten integers in an array and
the program will do the
following
Find the average of even marks
Find how many prime integers inserted
Note: please solve it by c , not c++
#include <stdio.h>
int prime(int arr[])
{
int i,j,count,k;
count=0;
for(i=0;i<10;i++)
{
k=0;
for(j=1;j<=arr[i];j++)
{
//counting the number of factors for the array element
if(arr[i]%j==0)
{
k++;
}
}
//if the no of factor is equal to 2
//it will count it as prime
if(k==2)
{
count++;
}
}
//returning the number of prime numbers
return count;
}
int main()
{
int arr[10];
int i,count,sum;
//taking the array inputs
printf("Enter ten array elements of integer type .\n");
for(i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
sum=count=0;
//calculating sum for the array index of even positions
for(i=0;i<10;i=i+2)
{
sum=sum+arr[i];
count++;
}
//printing the corresponding average
printf("The average of the even marks : %d\n",(sum/count));
sum=count=0;
//calculating sum for the array elements which are even
for(i=0;i<10;i++)
{
if(arr[i]%2==0)
{
sum=sum+arr[i];
count++;
}
}
//printing the corresponding average
printf("The average of the even numbers : %d\n",(sum/count));
//calling the function to count
//the number of prime present in the array
count=prime(arr);
printf("The number of prime numbers present in the array is : %d\n",count);
return 0;
}