Odd Numbers 1-N Recursively
Print odd numbers from 1 to n recursively.
IntermediatePhase 3 - Level 1: Basic RecursionExample 5 of 10
odd-numbers-1-n-recursively.py
1def print_odd(n):2 # Base case3 if n == 0:4 return56 # Recursive case7 print_odd(n - 1)8 # Print if odd9 if n % 2 != 0:10 print(n)1112# Test13n = int(input("Enter n: "))14print_odd(n)
Output
Enter n: 10 1 3 5 7 9
What's going on
Recurse first, then check and print if odd.
Key Concepts:
Recurse to smaller problem
Check if current number is odd
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