Print Triangle (Bottom-Up) Recursively

Print inverted triangle recursively.

Logic BuildingIntermediate
Logic Building
def print_stars(n):
    if n == 0:
        return
    print("*", end="")
    print_stars(n - 1)

def print_triangle(n, row=1):
    # Base case
    if row > n:
        return
    
    # Recurse first
    print_triangle(n, row + 1)
    # Then print current row
    print_stars(row)
    print()

# Test
n = int(input("Enter rows: "))
print_triangle(n)

Output

Enter rows: 5
*****
****
***
**
*

Recurse first, then print (reverse order).

Key Concepts:

  • Recurse before printing
  • Prints in reverse order
  • Creates inverted triangle