You are on page 1of 5

Algorithm for

Count number of odd , even and


zeros in list of integers.

Ms. Saraswati R Bhusanur


Algorithm: Count Odd, Even, and Zero Elements
Input: Accept a list of integers arr.

Initialize Counters:Initialize three counters: odd_count,


even_count, and zero_count to 0.

Iterate Through the List:For each element num in the list:


If num is zero, increment zero_count.
Else if num is even, increment even_count.
Else increment odd_count

Output Counts:
Display or use the values of odd_count, even_count, and
zero_count.
1 0 2 4 0 3 7 11 54 23 14 9 8 7 0 5 41 21 0 22

For Zeros For even For odd


odd_count = even_count = zero_count = 0.

1 2 4 0 3 11

Zero, then zero_count = zero_count+1

is this Even number , then even_count = even_count + 1


number

Odd number , then odd_count = odd_count + 1


1 2 4 0 3 11

Iteration 1  odd_count is increases = 0+1 = 1


Final Values
Iteration 2  even_count is increases = 0+1 = 1
odd_count = 3
Iteration 3  even_count is increases = 1+1 = 2
even_count = 2
Iteration 4  zero_count is increases = 0+1 = 1

Iteration 5  odd_count is increases = 1+1 = 2


zero_count = 1
Iteration 6  odd_count is increases = 2+1 = 3

You might also like