Even Numbers 1-N Recursively
Print even numbers from 1 to n recursively.
IntermediatePhase 3 - Level 1: Basic RecursionExample 4 of 10
even-numbers-1-n-recursively.py
1def print_even(n):2 # Base case3 if n == 0:4 return56 # Recursive case7 print_even(n - 1)8 # Print if even9 if n % 2 == 0:10 print(n)1112# Test13n = int(input("Enter n: "))14print_even(n)
Output
Enter n: 10 2 4 6 8 10
What's going on
Recurse first, then check and print if even.
Key Concepts:
Recurse to smaller problem
Check if current number is even
Print if condition met
🔗Related Content
- 📖
Phase 3 - Learn Concepts
Review Phase 3 concepts and explanations
- 📝
Phase 3 - Quiz
Test your Phase 3 understanding with quiz questions
- 💻
Phase 3 - All Practice Problems
Explore all practice problems for Phase 3
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum