Increasing + Decreasing in One Function

Print numbers increasing then decreasing recursively.

Logic BuildingAdvanced
Logic Building
def print_inc_dec(n, current=1):
    # Base case
    if current > n:
        return
    
    # Print increasing
    print(current, end=" ")
    # Recurse
    print_inc_dec(n, current + 1)
    # Print decreasing (after recursion)
    if current < n:
        print(current, end=" ")

# Test
n = int(input("Enter n: "))
print_inc_dec(n)
print()

Output

Enter n: 5
1 2 3 4 5 4 3 2 1

Print before recursion (increasing), after (decreasing).

Key Concepts:

  • Print before recursion: increasing order
  • Print after recursion: decreasing order
  • Creates symmetric pattern