Print Alternating Numbers
Print numbers 1 to n with alternating signs.
BeginnerPhase 2 - Level 1: Basic LoopingExample 14 of 14
print-alternating-numbers.py
1# Take n as input2n = int(input("Enter n: "))34# Print alternating5for i in range(1, n + 1):6 if i % 2 == 0:7 print(-i, end=" ")8 else:9 print(i, end=" ")10print()
Output
Enter n: 5 1 -2 3 -4 5
What's going on
Alternate signs based on even/odd.
Key Concepts:
Check if number is even
Print negative for even, positive for odd
Create alternating pattern
🔗Related Content
- 📖
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- 📝
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- 💻
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum