In this post, you will learn how to write a Python program to print even numbers from 1 to 100, 1 to N, and in a given range using for-loop, while-loop, and function. But before writing the program let’s understand what are even numbers.
What are Even Numbers?
Even numbers are numbers that are divisible by 2.
For Example:
Algorithm to Print Even Numbers from 1 to 100
- Iterate using for-loop from range 0 to 100 (
for i in range(0, 101)
) - Inside the for-loop check
if i % 2 == 0
thenprint(i)
(Becausei
is an even number) - End the program.
From the above algorithm, we understood how to implement a Python program to print even numbers from 1 to 100. So now it’s time to do it practically.
Write a Python Program to Print Even Numbers from 1 to 100 Using a for-loop
Before writing this program few programming concepts you have to know:
Source Code:
for i in range(0,101):
if i % 2 == 0:
print(i, end=",")
Output:
0,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,
Write a Python Program to Print Even Numbers from 1 to 100 Using a while-loop
Before writing this program few programming concepts you have to know:
Source Code:
i = 0
while i <= 100:
if i % 2 == 0:
print(i, end=",")
i+=1
Output:
0,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,
Write a Python Program to Print Even Numbers from 1 to 100 Using a Function
Before writing this program few programming concepts you have to know:
Source Code:
def even_100():
for i in range(0, 100+1):
if i%2 == 0:
print(i,end=",")
even_100() # calling a function
Output:
0,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,
Write a Python Program to Print Even Numbers from 1 to N Using a for-loop
Algorithm:
- Take the input from the user (
num
) - Iterate using for-loop from range 0 to num (
for i in range(0, num+1)
) - Inside the for-loop check
if i % 2 == 0
thenprint(i)
(Becausei
is an even number) - End the program.
From the above algorithm, we understood how to implement a Python program to print even numbers from 1 to N. but before writing this program few programming concepts you have to know.
Source Code:
num = int(input("Enter a Number: "))
for i in range(0,num+1):
if i % 2 == 0:
print(i, end=",")
Output:
Enter a Number: 10
0,2,4,6,8,10,
Write a Python Program to Print Even Numbers from 1 to N Using a while-loop
Before writing this program few programming concepts you have to know:
Source Code:
num = int(input("Enter a Number: "))
i = 0
while i <= num:
if i % 2 == 0:
print(i,end=",")
i+=1
Output:
Enter a Number: 20
0,2,4,6,8,10,12,14,16,18,20,
Write a Python Program to Print Even Numbers from 1 to N Using a Function
Before writing this program few programming concepts you have to know:
Source Code:
def even_numbers(N):
for i in range(0, N+1):
if i%2 == 0:
print(i,end=",")
num = int(input("Enter a Number: "))
even_numbers(num)
Output:
Enter a Number: 50
0,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,
Write a Python Program to Print Even Numbers in a given range Using a for-loop
Before writing this program few programming concepts you have to know:
Source Code:
num1 = int(input("Enter first Number: ")) #lower range
num2 = int(input("Enter second Number: ")) # upper range
for i in range(num1, num2+1):
if i % 2 == 0:
print(i, end=",")
Output:
Enter first Number: 10
Enter second Number: 20
10,12,14,16,18,20,
Write a Python Program to Print Even Numbers in a given range Using a while-loop
Before writing this program few programming concepts you have to know:
Source Code:
num1 = int(input("Enter first Number: ")) #lower range
num2 = int(input("Enter second Number: ")) # upper range
while num1 <= num2:
if num1 % 2 == 0:
print(num1, end=",")
num1+=1
Output:
Enter first Number: 50
Enter second Number: 100
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,
Write a Python Program to Print Even Numbers in a given range Using a Function
Before writing this program few programming concepts you have to know:
Source Code:
def Even_range(n1,n2):
for i in range(n1, n2+1):
if i % 2 == 0:
print(i, end=",")
num1 = int(input("Enter first Number: ")) #lower range
num2 = int(input("Enter second Number: ")) # upper range
Even_range(num1,num2)
Output:
Enter first Number: 25
Enter second Number: 50
26,28,30,32,34,36,38,40,42,44,46,48,50,