Count Even and Odd Elements
Count even and odd elements in array.
BeginnerPhase 4 - Level 1: Basic ArraysExample 6 of 10
count-even-and-odd-elements.py
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Count8even = 09odd = 01011for element in arr:12 if element % 2 == 0:13 even += 114 else:15 odd += 11617print(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
What's going on
Check each element for even/odd.
Key Concepts:
Use modulo to check even/odd
Increment respective counter
Print counts
🔗Related Content
- 📖
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- 📝
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- 💻
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum