Counting the number of even and odd elements in an array in Python

Program to count the number of even and odd elements in an array

Here we will going to learn about the process of counting the number of even and odd elements in an array which is discussed here. Given an array of integers, count the total number of odd elements and even elements in the array and display them as output.

Example

Input – 1,2,3,4

output-

even elements-2

odd elements- 2

count even and odd number

Algorithm

  • Input the array elements separated by space
  • Initialize count_odd = count_even = 0.
  • Traverse the array and increment count_even if the array element is odd, else increment count_odd.
  • Print count_odd and count_even.

 

Python code

n=list(map(int,input().split()))
count_odd=0
count_even=0
for i in n:
if i%2==0:
count_even=count_even+1
else:
count_odd=count_odd+1
print("Even elements:",count_even)
print("Odd elements:",count_odd)
1 2 3
Even elements: 1
Odd elements: 2

Comments