Sum and Count of Even and Odd Numbers in an Array

Here we will write a C program to find the sum of an array element and count of even and odd numbers in the given list of N numbers.

Program description:- Write a program to find the sum and count of even and odd numbers in the given list of N number.

int main()
   int a[100], n, i, sum=0, ecount=0, ocount=0;

   printf("Enter array size [1-100]: ");
   printf("Enter %d elements: ",n);

   for(i=0; i<n; i++)
     sum += a[i];
     if(a[i]%2==0) ecount++;
     else ocount++;

   printf("Total Sum: %d",sum);
   printf("\nEven numbers: %d",ecount);
   printf("\nOdd numbers = %d", ocount);

   return 0;

Output for the different test-cases:-

Enter array size [1-100]: 5
Enter 5 elements: 1 2 3 4 5
Total Sum: 15
Even numbers: 2
Odd numbers = 3

Enter array size [1-100]: 7
Enter 7 elements: -9 -8 -5 0 4 7 3
Total Sum: -8
Even numbers: 3
Odd numbers = 4

If the number is divisible by 2 then the number is even else the number is odd. The variable sum, ecount, and ocount is initialized with zero. The variable sum stores the sum of all elements. When the number is even than the variable ecount is incremented else the variable ocount is incremented.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Similar C programming examples on Arrays

Leave a Comment

Your email address will not be published. Required fields are marked *