Check if a Number is Even or Odd
Check if a number is even or odd using modulo operator.
BeginnerPhase 1 - Level 1: Simple ConditionsExample 2 of 10
check-if-a-number-is-even-or-odd.py
1# Take a number as input2num = int(input("Enter a number: "))34# Check if even or odd5if num % 2 == 0:6 print("Even")7else:8 print("Odd")
Output
Enter a number: 8 Even Enter a number: 7 Odd
What's going on
The modulo operator (%) returns the remainder after division.
Key Concepts:
num % 2 gives remainder when num is divided by 2If remainder is 0, number is even
If remainder is 1, number is odd
Logic:
Even numbers: 0, 2, 4, 6, 8, 10, ...
Odd numbers: 1, 3, 5, 7, 9, 11, ...
🔗Related Content
- 📖
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- 📝
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- 💻
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum