Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Count Even and Odd Elements

Count even and odd list elements with Python remainder tests. Students classify every integer once and distinguish the count of matches from the sum of values.

BeginnerPhase 4 - Level 1: Basic ArraysExample 6 of 10
count-even-and-odd-elements.py
1# Take array
2n = int(input("Enter array size: "))
3arr = []
4for i in range(n):
5 arr.append(int(input(f"Element {i+1}: ")))
6
7# Count
8even = 0
9odd = 0
10
11for element in arr:
12 if element % 2 == 0:
13 even += 1
14 else:
15 odd += 1
16
17print(f"Even: {even}, Odd: {odd}")

Output

Enter array size: 5
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5
Element 5: 6
Even: 3, Odd: 2

In this one

  • Count values by parity
  • Include zero
  • Distinguish frequency from total

How it works

  1. 1

    Collect integers and initialize even and odd.

  2. 2

    Test each value modulo two.

  3. 3

    Increment one counter and print both totals.

Edge cases

Is zero even here?

Is zero even here?

Yes. Its remainder modulo two is zero.

What about an empty list?

What about an empty list?

Both counters stay zero.

What's going on

Each collected element is tested with element % 2 == 0. A match increments even; the other branch increments odd.

Zero belongs to the even count, and negative integers are classified correctly by their remainder. Repeated values each contribute a count because this measures list entries. The two counters together account for every element without changing the list.

Is zero even here? Yes. Its remainder modulo two is zero.

What about an empty list? Both counters stay zero.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is incremented for a match?

Pick an answer to check it.

Question 2

Is zero even here?

Pick an answer to check it.

Questions people ask

Is zero even here?

Yes. Its remainder modulo two is zero.

What about an empty list?

Both counters stay zero.

What is incremented for a match?

A counter by one.