Write a C program to count even and odd elements in an array

Write a c program to count even and odd elements in an array, In this c program example, we will see how to count even and odd elements in an array by using for loop and if-else statement.

Sample input

enter the size of an array: 10

enter the elements:

21
22
23
24
25
26
27
28
29
30

Sample output

total even number: 5
total odd number: 5

What is an even and odd element (number)?

Even element:
The element (number) which can divide by two and the remainder remains zero is known as even element(number).
Odd element:
The element (number) which can divide by two and the remainder remains non-zero is known as even element(number).

Algorithm to count even and odd elements

  • Declare an array, declare one variable, and initializes two variables.
  • Then take two inputs one for the size of the array and the second in the loop for elements.
  •  Take a two-for loop.
  • In the first loop take input and in the second loop take an if-else statement.
  • Print a suitable message.

C program to count even and odd elements

#include<stdio.h>
void main(){
  int a[10];
  int i,even=0,odd=0,n;
    printf("entre the size of an array : ");
    scanf("%d",&n);

     printf(" \nenter %d elements in the array :\n",n);
  for(i=0;i<n;i++){
   
     scanf("%d",&a[i]);	
  
   }
  for(i=0;i<n;i++){
    
    if(a[i]%2==0){
      even++;
    }
    else
     odd++;
  }
  printf(" \ntotal even number : %d \ntotal odd number : %d",even,odd);
}

 

Explanation of this program

Step 1: Start.

Step 2: Create a hadder file and include the library on it.

Step 3: Then create a void main function.

Step 4: Declare an array (a[10]) and one variable (i,n) and intilaize two variable (even=0,odd=0).

step 5: create two inputs, first for the size of the array another in for the loop of elements.

Step 6: Create a for loop and give condition(i=0;i<n;i++) and in loop crate an input with the help of scanf to take input (number) from user to count even and odd elements.

Step 7: create another for loop and in this loop create if-else statement and give the condition and increment to both loop and statement like:
-(i=0;i<n;i++)= for loop,
-(a[i]%2==0)= if statement,
-even++ = increment for new element,
-odd++ = increment for new element in else statement.

Step 8: print a suitable output with the help of print.

Step 9: End.

Output

 

 

In this way, we will see how to write a C program to count an even and odd element in an array.