Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python program to count numbers of even and odd elements in an array

Count numbers of Even and Odd elements in an Array

Count number of even and odd elements in an array

In this page we are going to count number of even and odd elements in an array using python. let’s see how to find whether a number is even or odd

Any number divisible by 2 is even and which is not divisible by 2 is odd number. We can use % operator to find whether it is divisible by 2 or not. By this we can count number of even and odd elements in an array using python. See the example for better understand

Example:

Input:

  • size of array- 6
  • array elements- 12 34 56 89 70 35

Output:

  • number of even elements- 4
  • number of odd elements- 2

Explanation 

  • 12, 34, 56, 70 are even elements 
  • 89, 35 are odd elements 

Algorithm:

  • Input the number of elements of the array.
  • Input the array elements.
  • Initialize even_count = odd_count = 0.
  • Traverse the array and increment odd_count if the array element is odd, else increment even_count.
  • Print even_count and odd_count.

Python code:

n=int(input(“Enter array size :\n))
arr=[]
for i in range(n):
    arr.append(int(input()))
even_count,odd_count=0,0
for i in arr:
    if(i%2==0):
        even_count+=1
    else:
        odd_count+=1
print(“Number of even elements\n,even_count)
print(“Number of odd elements\n,odd_count)

Input:

Enter array size:
6
Enter array elements:
1
2
3
4
5
7

Output:

Number of even elements
 2
Number of odd elements
 4

JAVA program to count number of even and odd elements in an array