Print Square of Stars Recursively

Print square pattern using recursion.

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

def print_square(n, row=0):
    # Base case
    if row == n:
        return
    
    # Print row
    print_stars(n)
    print()
    # Recurse for next row
    print_square(n, row + 1)

# Test
n = int(input("Enter size: "))
print_square(n)

Output

Enter size: 4
****
****
****
****

Print row of n stars, then recurse for next row.

Key Concepts:

  • Use helper function for printing stars
  • Print row, then recurse
  • Track row number with parameter