Write a python program to print all even numbers between 1 to 100
In this post, you will learn how to write a Python program to print all even numbers between 1 to 100 using the for loop, while loop and if-else statement. Such a type of question is generally asked in a logical programming interview.
Python program to 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
Python program to 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
Python program to 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 <= max:
if(num % 2 == 0):
print("{0}".format(num))
num = num + 1
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
Python program to print even numbers between 1 to 100 using a while loop without if statement
In the given Python program, we have used a while loop to check weather the num variable is less than or equal to 100. If the condition is satisfied, then only the rest of the code will be executed, else not.
num = 2
while num <= 100:
print(num)
num = num + 2
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
Related Articles
Print first 10 natural numbers using while loop in Python
Python program to convert month name to a number of days
Python program to calculate number of days between two dates
Sum of digits of a number in Python
Python program to calculate the average of numbers in a given list
Find element in list Python
Python program to check leap year
Python program to reverse a string
Pascal triangle pattern in Python
Permutation and Combination in Python
Check if two strings are anagrams Python
Python program to find area of circle
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Count consonants in a string Python
Convert array to list Python