Open In App

Python program to print all even numbers in a range

Improve
Improve
Like Article
Like
Save
Share
Report

Given starting and end points, write a Python program to print all even numbers in that given range. 

Example:

Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14

Input: start = 8, end = 11
Output: 8, 10

Example #1: Print all even numbers from the given list using for loop Define start and end limit of range. Iterate from start till the range in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number. 

Python3




# Python program to print all even numbers  in range
for even_numbers in range(4,15,2):
  #here inside range function first no denotes starting,
  #second denotes end and
  #third denotes the interval
    print(even_numbers,end=' ')


Output

4 6 8 10 12 14 

Time Complexity: O(n)
Auxiliary Space: O(1)

  Example #2: Taking range limit from user input 

Python3




# Python program to print Even Numbers in given range
 
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
 
# iterating each number in list
for num in range(start, end + 1):
 
    # checking condition
    if num % 2 == 0:
        print(num, end=" ")


Output:

Enter the start of range: 4
Enter the end of range: 10
4 6 8 10 

The time complexity of this program is O(n), where n is the number of integers in the range. 

The space complexity of this program is also O(1), which is constant. 

Example#3 Taking range limit user input and using skip sequence number in range function which generates the all-even number. 

Python3




# Python program to print Even Numbers in given range
 
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
 
#creating even starting range
start = start+1 if start&1 else start
  
 
#create a list and printing element
#contains Even numbers in range
[ print( x ) for x in range(start, end + 1, 2)]


Output:

Enter the start of range: 4
Enter the end of range: 10
4 6 8 10 

The time complexity of this program is O(n), where n is the number of integers in the range. 

The space complexity of this program is also O(1), which is constant.

Method: Using recursion 

Python3




def even(num1,num2):
    if num1>num2:
        return
    print(num1,end=" ")
    return even(num1+2,num2)
num1=4;num2=15
even(num1,num2)


Output

4 6 8 10 12 14 

Time Complexity:
The time complexity of the program is O(N/2), where N is the difference between num2 and num1 since the program only checks and prints even numbers between num1 and num2. 

Space Complexity:
The program uses a constant amount of extra space, regardless of the size of the input range. Therefore, the space complexity is O(1).

Method: Using the lambda function 

Python3




# Python code To print all even numbers
# in a given range using the lambda function
a=4;b=15
li=[]
for i in range(a,b+1):
    li.append(i)
# printing odd numbers using the lambda function
even_num = list(filter(lambda x: (x%2==0),li)) 
print(even_num)


Output

[4, 6, 8, 10, 12, 14]

Time Complexity: time complexity of the program is O(N).

Space Complexity:
The program creates a list of integers between a and b, which takes O(N) space in the worst case. Then, it creates a new list to store even numbers returned by the filter function. The space used by this new list depends on the number of even numbers in the input range, which is at most N/2. Therefore, the space complexity of the program is O(N).

Method: Using list comprehension 

Python3




x=[i for i in range(4,15+1) if i%2==0]
print(*x)


Output

4 6 8 10 12 14

Method: Using enumerate function 

Python3




a=4;b=15;l=[]
for i in range(a,b+1):
  l.append(i)
print([a for j,a in enumerate(l) if a%2==0])


Output

[4, 6, 8, 10, 12, 14]

Method: Using pass 

Python3




a=4;b=15
for i in range(a,b+1):
  if i%2!=0:
    pass
  else:
    print(i,end=" ")


Output

4 6 8 10 12 14 

Time Complexity: O(b-a)

Method: Using Numpy.Array

Python3




# Python code To print all even numbers
# in a given range using numpy array
import numpy as np
 
# Declaring Range
a=4;b=15
li= np.array(range(a, b+1))
 
# printing odd numbers using numpy array
even_num = li[li%2==0];
print(even_num)


Output:

[ 4  6  8 10 12 14]

Method: Using bitwise and operator

Python3




# Python program to print even Numbers in given range
#using bitwise & operator
 
 
start, end = 4, 19
 
 
 
# iterating each number in list
 
for num in range(start, end + 1):
 
     
 
    # checking condition
 
    if not (num & 1):
 
        print(num, end = " ")
 
#this code is contributed by vinay pinjala.


Output

4 6 8 10 12 14 16 18 

Time Complexity: O(n)
Auxiliary Space:O(1)

Method: Using bitwise or operator

Algorithm:

  1. Take the start and end values for the range.
  2. Using a for loop, iterate through each number in the range.
  3. Check if the bitwise OR operation between the number and 1 is equal to the number itself.
  4. If the above condition is not satisfied, then it means the number is even, so print it.
  5. The loop continues until all the numbers in the range have been checked.

Python3




# Python program to print even Numbers in given range
#using bitwise | operator
 
 
start, end = 4, 19
 
 
 
# iterating each number in list
 
for num in range(start, end + 1):
 
     
 
    # checking condition
 
    if not (num | 1)==num:
 
        print(num, end = " ")
 
#this code is contributed by tvsk


Output

4 6 8 10 12 14 16 18 

Time complexity: O(n)
The time complexity of this code is O(n) because it iterates through each number in the given range once.

The auxiliary space of this program is O(1) ,because the amount of memory used by the program remains constant regardless of the size of the input. The program only uses a few variables to store the start and end values, and the num variable used in the loop. As the input size increases, the number of iterations of the loop will increase, but the space used by the program remains the same.



Last Updated : 20 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads