# Python Program to Count Even and Odd Numbers in an Array

Write a Python Program to Count Even and Odd Numbers in an Array using for loop range. The if condition (if (evenOddArr[i] % 2 == 0)) checks whether the array item is divisible by two. If True, we add one to the even array count; otherwise, add one to the odd array count value.

# Using For loop

import numpy as np

evenOddArr = np.array([10, 99, 22, 50, 77, 22, 112, 19])
print("The List of Numbers in evenOddArr Array = ", evenOddArr)

evenArrCount = 0
oddArrCount = 0

for i in range(len(evenOddArr)):
if (evenOddArr[i] % 2 == 0):
evenArrCount = evenArrCount + 1
else:
oddArrCount = oddArrCount + 1

print("The Count of Even Numbers in evenOddArr Array = ", evenArrCount)
print("The Count of Odd Numbers in evenOddArr Array  = ", oddArrCount)

## Python Program to Count Even and Odd Numbers in an Array

In this Python example, we used the numpy mod and remainder functions to inspect each item’s remainder by two is equal to zero.

# using Numpy mod and remainder

import numpy as np

evenArr = np.array([4, 29, 88, 5, 0, 11, 17, 18, 7, 44, 9, 89])
evenArrCount = evenArrCount1 = evenArrCount2 = 0
oddArrCount = oddArrCount1 = oddArrCount2 = 0

for i in evenArr:
if (i % 2 == 0):
evenArrCount = evenArrCount + 1
else:
oddArrCount = oddArrCount + 1

print("The Count of Even Numbers in evenOddArr Array = ", evenArrCount)
print("The Count of Odd Numbers in evenOddArr Array  = ", oddArrCount)

print("\n=== Using mod function===")
for i in evenArr:
if (np.mod(i, 2) == 0):
evenArrCount1 = evenArrCount1 + 1
else:
oddArrCount1 = oddArrCount1 + 1

print("The Count of Even Numbers in evenOddArr Array = ", evenArrCount1)
print("The Count of Odd Numbers in evenOddArr Array  = ", oddArrCount1)

print("\n=== Using remainder function===")
for i in evenArr:
if (np.remainder(i, 2) == 0):
evenArrCount2 = evenArrCount2 + 1
else:
oddArrCount2 = oddArrCount2 + 1
print("The Count of Even Numbers in evenOddArr Array = ", evenArrCount2)
print("The Count of Odd Numbers in evenOddArr Array  = ", oddArrCount2)

Python Count Even and Odd Numbers in a Numpy Array output

The Count of Even Numbers in evenOddArr Array =  5
The Count of Odd Numbers in evenOddArr Array  =  7

=== Using mod function===
The Count of Even Numbers in evenOddArr Array =  5
The Count of Odd Numbers in evenOddArr Array  =  7

=== Using remainder function===
The Count of Even Numbers in evenOddArr Array =  5
The Count of Odd Numbers in evenOddArr Array  =  7

Python Program to Count Even and Odds in Numpy Array using the While Loop.

# Using while loop
import numpy as np
evenOddArr = np.array([4, 15, 19, 22, 9, 18, 140])
i = 0
evenArrCount = 0
oddArrCount = 0
while (i < len(evenOddArr)):
if (np.equal(evenOddArr[i] % 2, 0)):
evenArrCount = evenArrCount + 1
else:
oddArrCount = oddArrCount + 1
i = i + 1

print("The Count of Even Numbers in evenOddArr Array = ", evenArrCount)
print("The Count of Odd Numbers in evenOddArr Array  = ", oddArrCount)

Count Even and Odds in Python Numpy Array using the While Loop output

The Count of Even Numbers in evenOddArr Array =  4
The Count of Odd Numbers in evenOddArr Array  =  3

In this Python numpy array example, we created a (CountEvenOddNumbers(evenOddArr)) function that returns the count of Even and Odd numbers.

# Uisng Functions
import numpy as np
def CountEvenOddNumbers(evenOddArr):
evenArrCount = 0
oddArrCount = 0
for i in evenOddArr:
if (np.remainder(i, 2) == 0):
evenArrCount = evenArrCount + 1
else:
oddArrCount = oddArrCount + 1

return evenArrCount, oddArrCount

evenOddArr = np.array([11, 88, 15, 122, 140, 17, 10, 48, 35, 64])
even, odd = CountEvenOddNumbers(evenOddArr)
print("The Count of Even Numbers in evenOddArr Array = ", even)
print("The Count of Odd Numbers in evenOddArr Array  = ", odd)

Count Even and Odds in Numpy Array using functions output

The Count of Even Numbers in evenOddArr Array =  6
The Count of Odd Numbers in evenOddArr Array  =  4