CoursesCSS StudioExamplesGATECompaniesPrepMentora
Programs/Logic building/Phase 2 - Level 1: Basic Looping

14 / 14

Topic
1. Print Numbers from 1 to 102. Print All Even Numbers Between 1 and 1003. Sum of First N Natural Numbers4. Print the Factorial of a Given Number5. Print All Odd Numbers Between 1 and 1006. Print Numbers from 10 Down to 17. Print Multiplication Table8. Sum of All Even Numbers Up to N9. Sum of All Odd Numbers Up to N10. Product of Digits11. Count Numbers Divisible by 312. Print Multiples of 5 Up to N13. Sum of Squares Up to N14. Print Alternating Numbers

Phase 2 - Level 1: Basic Looping

  1. 01Print Numbers from 1 to 10
  2. 02Print All Even Numbers Between 1 and 100
  3. 03Sum of First N Natural Numbers
  4. 04Print the Factorial of a Given Number
  5. 05Print All Odd Numbers Between 1 and 100
  6. 06Print Numbers from 10 Down to 1
  7. 07Print Multiplication Table
  8. 08Sum of All Even Numbers Up to N
  9. 09Sum of All Odd Numbers Up to N
  10. 10Product of Digits
  11. 11Count Numbers Divisible by 3
  12. 12Print Multiples of 5 Up to N
  13. 13Sum of Squares Up to N
  14. 14Print Alternating Numbers

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 input
2n = int(input("Enter n: "))
3
4# Print alternating
5for 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

PreviousSum of Squares Up to N

On this page

CodeOutputExplanation
Schoolabe

Learn, Practice, Ace Coding

ExamplesCompaniesTutorialsCSS StudioDSA VisualizersQuizInterview PrepMentoraCareer QuestGATE CS prepGATE CS 2027 study planJob ReadyInternships
AboutContactCareersAffiliate programBlog
DocumentationAPI ReferenceCommunitySupport
Privacy PolicyTermsRefund PolicyCookie Policy

© 2026 Schoolabe