Logic Building
def print_n_to_1(n):
# Base case
if n == 0:
return
# Recursive case: print current, then smaller
print(n)
print_n_to_1(n - 1)
# Test
n = int(input("Enter n: "))
print_n_to_1(n)Output
Enter n: 5 5 4 3 2 1
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