How to Print Even Numbers from 1 to N in Python 🐍


Print Even Numbers from 1 to N in Python

Printing even numbers between 1 and N is a simple but essential programming exercise that helps beginners practice loops and conditional statements in Python. In this blog, we’ll walk through several ways to print all even numbers from 1 to a given number N.

By the end of this post, you’ll learn:

  • What an even number is.
  • Different methods to print even numbers from 1 to N.
  • How loops and conditions work together in Python.

What is an Even Number?

An even number is an integer that can be divided exactly by 2, meaning there is no remainder when divided by 2. Some examples of even numbers are:

  • 2, 4, 6, 8, 10, 12, etc.

In contrast, numbers like 1, 3, 5, 7, and 9 are odd numbers because they leave a remainder when divided by 2.


How to Print Even Numbers from 1 to N in Python

There are several ways to approach this problem in Python. We’ll cover the following methods:

  1. Using a for loop with an if statement.
  2. Using a for loop with a step value.
  3. Using a while loop.

Method 1: Using a For Loop with an If Statement

The simplest way to print even numbers is by using a for loop to iterate through the numbers from 1 to N, and using an if statement to check whether each number is even.

# Program to print even numbers from 1 to N using a for loop

# Step 1: Take user input
N = int(input("Enter a number: "))

# Step 2: Use a for loop to iterate through numbers from 1 to N
for i in range(1, N + 1):
    # Step 3: Check if the number is even
    if i % 2 == 0:
        print(i, end=" ")

Explanation of the Code

  1. User Input:
  • We take the input number N from the user and convert it to an integer using int().
  1. For Loop:
  • The loop for i in range(1, N + 1) iterates from 1 to N.
  1. If Statement:
  • We use the condition if i % 2 == 0 to check if a number is even. If the remainder when dividing i by 2 is zero, the number is even.
  • The print(i, end=" ") statement prints all the even numbers on the same line, separated by spaces.

Sample Output

Enter a number: 10
2 4 6 8 10

In this example, all even numbers between 1 and 10 are printed.


Method 2: Using a For Loop with a Step Value

Python’s range() function allows you to specify a step value. You can use this feature to directly print even numbers by starting from 2 and incrementing by 2 in each iteration.

# Program to print even numbers from 1 to N using a step value

# Step 1: Take user input
N = int(input("Enter a number: "))

# Step 2: Use a for loop with a step of 2 to print even numbers
for i in range(2, N + 1, 2):
    print(i, end=" ")

Explanation of the Code

  1. User Input:
  • We take the input number N from the user.
  1. For Loop with Step Value:
  • The range(2, N + 1, 2) function generates numbers starting from 2 and increments by 2 in each iteration. This allows us to directly print all even numbers.
  1. Printing the Numbers:
  • Each even number is printed in the same line, separated by spaces.

Sample Output

Enter a number: 12
2 4 6 8 10 12

In this case, the for loop prints even numbers from 2 to 12 directly using the step value.


Method 3: Using a While Loop

You can also use a while loop to print even numbers by manually incrementing the value by 2 in each iteration.

# Program to print even numbers from 1 to N using a while loop

# Step 1: Take user input
N = int(input("Enter a number: "))

# Step 2: Initialize the counter variable
i = 2

# Step 3: Use a while loop to print even numbers
while i <= N:
    print(i, end=" ")
    i += 2  # Increment by 2 to get the next even number

Explanation of the Code

  1. User Input:
  • We take the input N from the user as an integer.
  1. While Loop:
  • The loop starts with i = 2 and continues as long as i <= N.
  • Inside the loop, we print the current value of i and increment i by 2 to move to the next even number.

Sample Output

Enter a number: 7
2 4 6

In this case, the while loop prints the even numbers from 2 to 6 since 7 is not even.


Handling Edge Cases

Here are some special cases to consider:

  1. N = 0:
  • If N is 0, no numbers will be printed because there are no even numbers up to 0.
  1. Negative Numbers:
  • If N is negative, the program can be modified to either restrict the input to positive numbers or handle negative values accordingly.

Real-Life Applications of Even Numbers

Even numbers are often used in various real-world scenarios:

  • Mathematical Operations: Even numbers are fundamental in many arithmetic and algebraic operations.
  • Data Structures: In programming, even numbers are often used to split data or allocate memory evenly in data structures.
  • Pattern Design: Even numbers play a significant role in designing grids, layouts, and patterns in programming and graphics.

Conclusion

In this blog, we explored several methods to print even numbers from 1 to N in Python. We used a for loop with an if statement, a for loop with a step value, and a while loop to achieve the same result. These exercises not only help you practice loops and conditionals but also build your problem-solving skills in Python.


Stay tuned for more Python tutorials, where we dive into exciting problems and solutions! 😊

Share with our team

Leave a Comment