C Programming:
Write a function that takes in an array of integers and an integer containing the count of elements, then have it returns the sum of all the even values inside the array.
Code:
#include<stdio.h>
int sum_even(int a[], int size);
void main()
{
int Size, i, a[10];
int result = 0;
clrscr();
printf("Please Enter the Size of an Array : ");
scanf("%d", &Size);
printf("\nPlease Enter the Array Elements\n");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
result = sum_even(a, Size);
printf("\n The sum of Even Numbers in this Array = %d ", result);
getch();
}
int sum_even(int a[], int size) {
int j;
int sum = 0;
for(j=0; j<size; j++)
{
if(a[j]%2==0)
{
sum=sum+a[j];
}
}
return sum;
}
Screenshot of output:
Get Answers For Free
Most questions answered within 1 hours.