Write a function countEven(int*, int) which receives an integer array and its size, and returns the number of even numbers in the array.
#include <stdio.h>
#include <stdlib.h>
int countEven(int* integerArray, int size)
{
int counter = 0;
int i;
for(i = 0; i < size; i++)
{
if(integerArray[i]%2==0)
counter++;
}
return counter;
}
int main()
{
int size = 5;
int* integerArray = (int*)malloc(size * sizeof(int));
if(integerArray!=NULL)
{
int i;
for(i = 0; i < size; i++)
integerArray[i] = i+1;
printf("Array : ");
for(i = 0; i < size; i++)
printf("%d ", integerArray[i]);
printf("\n");
printf("count even numbers: %d", countEven(integerArray, size));
free(integerArray);
}
return 0;
}
Comments
Leave a comment