Print Triangle (Bottom-Up) Recursively
Print inverted triangle recursively.
IntermediatePhase 3 - Level 3: Pattern RecursionExample 4 of 10
print-triangle-bottom-up-recursively.py
1def print_stars(n):2 if n == 0:3 return4 print("*", end="")5 print_stars(n - 1)67def print_triangle(n, row=1):8 # Base case9 if row > n:10 return1112 # Recurse first13 print_triangle(n, row + 1)14 # Then print current row15 print_stars(row)16 print()1718# Test19n = int(input("Enter rows: "))20print_triangle(n)
Output
Enter rows: 5 ***** **** *** ** *
What's going on
Recurse first, then print (reverse order).
Key Concepts:
Recurse before printing
Prints in reverse order
Creates inverted triangle
🔗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