Sie sind auf Seite 1von 4

3/27/2018 C program to count even and odd elements in array - Codeforwin

A blog about computer science, for computer science learner – by computer science lover.

C program to count even and odd


elements in array

Write a C program to input elements in array from user and count even and odd elements in array. How to
find total number of even and odd elements in a given array using C programming. Logic to count even
and odd elements in array using loops.

Example

Input

Input array: 1 2 3 4 5 6 7 8 9

Output

Total even elements: 4


Total odd elements: 5

Required knowledge
Basic Input Output, If else, Loop, Array

Logic to count even and odd elements in


array
https://codeforwin.org/2016/09/c-program-to-count-even-odd-elements-in-array.html 1/4
3/27/2018 C program to count even and odd elements in array - Codeforwin

Step by step descriptive logic to count total even and odd elements in array.

1. Input size and elements in array from user. Store it in some variable say size and arr .

2. Declare and initialize two variables with zero to store even and odd count. Say even = 0 and
odd = 0 .

3. Iterate through each array element. Run a loop from 0 to size - 1 . Loop structure should look
like for(i=0; i<size; i++) .

4. Inside loop increment even count by 1 if current array element is even. Otherwise increment the
odd count.

Learn di erent ways of checking even odd.


Program to check even odd using if else.
Program to check or odd using conditional statement.
Program to check even odd using switch case.
Program to check even odd using bitwise operator.

5. Print the values of even and odd count a er the termination of loop.

Program to count even and odd elements in


array

1 /**
2 * C program to count total number of even and odd elements in an array
3 */
4
5 #include <stdio.h>
6
7 #define MAX_SIZE 100 //Maximum size of the array
8

https://codeforwin.org/2016/09/c-program-to-count-even-odd-elements-in-array.html 2/4
3/27/2018 C program to count even and odd elements in array - Codeforwin

9 int main()
10 {
11 int arr[MAX_SIZE];
12 int i, size, even, odd;
13
14 /* Input size of the array */
15 printf("Enter size of the array: ");
16 scanf("%d", &size);
17
18 /* Input array elements */
19 printf("Enter %d elements in array: ", size);
20 for(i=0; i<size; i++)
21 {
22 scanf("%d", &arr[i]);
23 }
24
25 /* Assuming that there are 0 even and odd elements */
26 even = 0;
27 odd = 0;
28
29 for(i=0; i<size; i++)
30 {
31 /* If the current element of array is even then increment even coun
32 if(arr[i]%2 == 0)
33 {
34 even++;
35 }
36 else
37 {
38 odd++;
39 }
40 }
41
42 printf("Total even elements: %d\n", even);
43 printf("Total odd elements: %d", odd);
44
45 return 0;
46 }

 Output   

Enter size of the array: 10


Enter 10 elements in array: 5 6 4 12 19 121 1 7 9 63
Total even elements: 3
Total odd elements: 7

https://codeforwin.org/2016/09/c-program-to-count-even-odd-elements-in-array.html 3/4
3/27/2018 C program to count even and odd elements in array - Codeforwin

Happy coding

Recommended posts
Array and matrices programming exercises index.
C program to put even and odd elements in two separate array.
C program to count total negative elements in an array.
C program to count total number of duplicate elements in an array.
C program to count frequency of each element in a given array.
C program to sort even and odd elements separately.
C program to print all unique elements in a given array.

 Previous Next 

Write your doubts or suggestion. I will try my best to help.


You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>

https://codeforwin.org/2016/09/c-program-to-count-even-odd-elements-in-array.html 4/4

Das könnte Ihnen auch gefallen