Print Alternating Numbers

Print numbers 1 to n with alternating signs.

Logic BuildingBeginner
Logic Building
# Take n as input
n = int(input("Enter n: "))

# Print alternating
for i in range(1, n + 1):
    if i % 2 == 0:
        print(-i, end=" ")
    else:
        print(i, end=" ")
print()

Output

Enter n: 5
1 -2 3 -4 5

Alternate signs based on even/odd.

Key Concepts:

  • Check if number is even
  • Print negative for even, positive for odd
  • Create alternating pattern