Print N to 1 Recursively
Print numbers from n to 1 using recursion.
IntermediatePhase 3 - Level 1: Basic RecursionExample 3 of 10
print-n-to-1-recursively.py
1def print_n_to_1(n):2 # Base case3 if n == 0:4 return56 # Recursive case: print current, then smaller7 print(n)8 print_n_to_1(n - 1)910# Test11n = int(input("Enter n: "))12print_n_to_1(n)
Output
Enter n: 5 5 4 3 2 1
What's going on
Print current number, then recurse with n-1.
Key Concepts:
Base case: n == 0, return
Print n first, then recurse
Decreases from n to 1
🔗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