0
0
PythonProgramBeginner · 2 min read

Python Program to Print Even Numbers from 1 to n

You can print even numbers from 1 to n in Python using a for loop and the condition if i % 2 == 0, like this: for i in range(1, n+1): if i % 2 == 0: print(i).
📋

Examples

Input5
Output2 4
Input10
Output2 4 6 8 10
Input1
Output
🧠

How to Think About It

To print even numbers from 1 to n, think about checking each number in this range to see if it divides evenly by 2. If it does, it is even and should be printed. We start from 1 and go up to n, testing each number with the % operator to find the remainder when divided by 2. If the remainder is zero, the number is even.
📐

Algorithm

1
Get the input number n.
2
Start a loop from 1 to n inclusive.
3
For each number, check if it is divisible by 2 (remainder zero).
4
If yes, print the number.
5
End the loop after reaching n.
💻

Code

python
n = int(input("Enter the value of n: "))
for i in range(1, n + 1):
    if i % 2 == 0:
        print(i)
Output
Enter the value of n: 10 2 4 6 8 10
🔍

Dry Run

Let's trace the program with input n = 5 to see how it prints even numbers.

1

Input

User enters n = 5

2

Loop start

i starts at 1

3

Check if i is even

1 % 2 = 1 (not zero), so skip printing

4

Next i

i = 2, 2 % 2 = 0, print 2

5

Continue loop

i = 3, 3 % 2 = 1, skip; i = 4, 4 % 2 = 0, print 4; i = 5, 5 % 2 = 1, skip

6

Loop ends

All numbers checked up to 5

ii % 2Print?
11No
20Yes (2)
31No
40Yes (4)
51No
💡

Why This Works

Step 1: Loop through numbers

The program uses a for loop to go through every number from 1 to n.

Step 2: Check even condition

It uses i % 2 == 0 to check if the number is even, meaning no remainder when divided by 2.

Step 3: Print even numbers

If the condition is true, it prints the number, so only even numbers appear in the output.

🔄

Alternative Approaches

Using range with step 2 starting from 2
python
n = int(input("Enter the value of n: "))
for i in range(2, n + 1, 2):
    print(i)
This method is more efficient because it only loops through even numbers directly.
Using list comprehension and join
python
n = int(input("Enter the value of n: "))
evens = [str(i) for i in range(1, n + 1) if i % 2 == 0]
print('\n'.join(evens))
This creates a list of even numbers first, then prints them all at once.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks each number from 1 to n once, so it runs in linear time O(n).

Space Complexity

It uses constant extra space O(1) because it prints numbers directly without storing them.

Which Approach is Fastest?

Using range(2, n+1, 2) is faster because it skips odd numbers, reducing loop iterations by half.

ApproachTimeSpaceBest For
Check each number with % 2O(n)O(1)Simple and clear logic
Range with step 2O(n/2)O(1)More efficient looping
List comprehensionO(n)O(n)When you need a list of even numbers
💡
Use range(2, n+1, 2) to loop only over even numbers for better efficiency.
⚠️
Beginners often forget to include the last number n by using range(1, n) instead of range(1, n+1).