Python program to print even numbers from 1 to 100 using for loop

In this post, you will learn how to write a Python program to print all even numbers between 1 to 100 using using for loop, while loop and if-else statement. Such a type of question is generally asked in a logical programming interview.

Print even numbers between 1 to 100 using a for loop

In the given Python program, we have iterated from start 1 to 100 using a loop and checked each value, if num % 2 == 0. If the condition is satisfied, i.e., num % 2 == 0 is true, then only print the number.

# Python program to print Even Numbers in given range
  
start, end = 1, 100
  
# iterating each number in list
for num in range[start, end + 1]:
      
    # checking condition
    if num % 2 == 0:
        print[num, end = " "]

Output of the above code:

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 

Print even numbers between 1 to 100 using a for loop without if statement

We started the range from 2 and used the counter value of 2 in the given Python program to print even numbers between 1 to 100.

# Python program to print Even Numbers from 1 to 100

max = 100

for num in range[2, max+1, 2]:
    print["{0}".format[num]]

Output of the above code:

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100

Print even numbers between 1 to 100 using a while loop

In the given Python program, we have applied the same logic as above, we just replaced the for loop with a while loop.

# Python program to print Even Numbers in given range
# using while loop

max = 100
num = 1

while num 

Chủ Đề