Open In App

Python program to print all odd numbers in a range

Last Updated : 22 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given starting and endpoints, write a Python program to print all odd numbers in that given range. 

Example:

Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15

Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11

Example #1: Print all odd numbers from the given list using for loop 

  1. Define the start and end limit of the range.
  2. Iterate from start till the range in the list using for loop and 
  3. check if num % 2 != 0. 
  4. If the condition satisfies, then only print the number. 

Python3




# Python program to print odd Numbers in given range
 
start, end = 4, 19
 
# iterating each number in list
for num in range(start, end + 1):
     
    # checking condition
    if num % 2 != 0:
        print(num, end = " ")


Output

5 7 9 11 13 15 17 19 

Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.

  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)


Output:

Enter the start of range: 3
Enter the end of range: 7
3
5
7

Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.

Example #3: Taking range limit from user input or with static inputs to reduce code execution time and to increase code performance.

Python3




# Uncomment the below two lines for taking the User Inputs
#start = int(input("Enter the start of range: "))
#end = int(input("Enter the end of range: "))
 
# Range declaration
start = 5
end = 20
 
if start % 2 != 0:
 
    for num in range(start, end + 1, 2):
        print(num, end=" ")
else:
 
    for num in range(start+1, end + 1, 2):
        print(num, end=" ")


Output

5 7 9 11 13 15 17 19 

Time Complexity: O(N), Here N is the difference of the end and start.
Auxiliary Space: O(1), As constant extra space is used.

Example #4: 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: "))
 
#create a list that contains only Even numbers in given range
even_list = range(start, end + 1)[start%2::2]
 
for num in even_list:
    print(num, end = " ")


Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 

Time Complexity: O(n)
Auxiliary Space: O(n), As constant extra space is used.

Method: Using the lambda function

Python3




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


Output

[3, 5, 7, 9, 11]

Time Complexity: O(n)
Auxiliary Space: O(1), As constant extra space is used.

Method: Using recursion 

Python3




def odd(num1,num2):
    if num1>num2:
        return
    if num1&1:
      print(num1,end=" ")
      return odd(num1+2,num2)
    else:
      return odd(num1+1,num2)
num1=5;num2=15
odd(num1,num2)


Output

5 7 9 11 13 15 

Time Complexity: O(n)
Auxiliary Space: O(1), As the function is tail recursive, no stack space is used.

Method: Using list comprehension

Python3




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


Output

5 7 9 11 13 15

Time Complexity: O(n)
Auxiliary Space: O(1), As constant extra space is used.

Method: Using the 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

[5, 7, 9, 11, 13, 15]

Time Complexity: O(n)
Auxiliary Space: O(1), As constant extra space is used.

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

5 7 9 11 13 15 

Time Complexity: O(n)
Auxiliary Space: O(1), As constant extra space is used.

Method: Using filter method:

Python3




# Python program to print Even Numbers in given range
  
# Range declaration
a=4;
b=15;
 
#create a list that contains only Even numbers in given range
l= filter(lambda a : a%2 , range(a, b+1))
print(*l)


Output

5 7 9 11 13 15

Time Complexity: O(n)
Auxiliary Space: O(1), As constant extra space is used.

Method: Using bitwise & operator  

Python3




# Python program to print odd 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 num & 1 != 0:
 
        print(num, end = " ")
 
 #this code is contributed  by vinay pinjala.


Output

5 7 9 11 13 15 17 19 

Time Complexity: O(n) 
Auxiliary Space: O(1), As constant extra space is used.

Method: Using bitwise | operator  

Python3




# Python program to print odd 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 num | 1 == num:
 
        print(num, end = " ")


Output

5 7 9 11 13 15 17 19 

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

Method : Using numpy module:

Algorithm:

1.Input the range of numbers, a and b.
2.Import the NumPy library.
3.Create an array containing numbers in the range using np.arange(a, b+1).
4.Apply a boolean filter to select only the even numbers using arr[arr % 2 != 0].
5.Print the resulting array of even numbers using print(*evens).

Python3




import numpy as np
 
# Range declaration
a = 3
b = 15
 
# Create an array containing numbers in the range
arr = np.arange(a, b+1)
 
# Create a new array containing only even numbers
evens = arr[arr % 2 != 0]
 
# Print the array of even numbers
print(*evens)
#This code is contributed by Jyothi pinjala


Output:

3 5 7 9 11 13 15

The time complexity :O(N), where N is the number of elements in the given range.

The auxiliary space :O(N), since it creates an array to hold all the numbers in the range, and then another array to hold only the even numbers.

Method: Using continue keyword

Approach:

  1. Define the start and end limit of the range.
  2. Iterate from start till the range in the list using for loop and 
  3. Check the condition, if (i % 2) == 0. 
  4. If condition satisfies, then skip the current iteration and increment the ith value.
  5. If condition doesn’t satisfies, then only print the number. 

Python3




a = 4
b = 15
for i in range(a, b+1):
    if i % 2 == 0:
        continue
    else:
        print(i, end=" ")
 
     # This Code is contributed by Pratik Gupta (guptapratik)


Output

5 7 9 11 13 15 

Time Complexity: O(n)
Auxiliary Space: O(1), as constant extra space is used.

Method: Using filterfalse method

Approach: 

  • Define the start and end limit of the range.
  • Using filterfalse method in the range between start and end limit. 
  • filterfalse method checks the condition, x%2==0.
  • Any number that satisfies the above condition is not a part of the result. 
  • The final result holds the number that is false in the above condition. 
  • print the final result. 

Python3




# Python 3 code to demonstrate
# print odd number in range
import itertools
 
# Range declaration
a = 3
b = 15
 
# using filterfalse function
evens = list(itertools.filterfalse(lambda x: x%2==0, range(a, b+1)))
 
# Print the array of even numbers
print(*evens)


Output:

3 5 7 9 11 13 15

Time Complexity: O(N), Here N is the range between starting and ending limit. 
Auxiliary Space: O(1), Because no extra space is used.



Similar Reads

Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices
Given a number N, the task is to calculate the count of numbers of length N having prime numbers at odd indices and odd numbers at even indices. Example: Input : N = 1Output: 5Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers. Input: N = 2Output: 20 Explanation: There are 20
5 min read
Make all the elements of array odd by incrementing odd-indexed elements of odd-length subarrays
Given an array arr[] of size N, the task is to make all the array elements odd by choosing an odd length subarray of arr[] and increment all odd positioned elements by 1 in this subarray. Print the count of such operations required. Examples: Input: arr[] = {2, 3, 4, 3, 5, 3, 2}Output: 2Explanation:In first operation, choose the subarray {2, 3, 4}
9 min read
Count numbers from given range having odd digits at odd places and even digits at even places
Given two integers L and R, the task is to count numbers from the range [L, R] having odd digits at odd positions and even digits at even positions respectively. Examples: Input: L = 3, R = 25Output: 9Explanation: The numbers satisfying the conditions are 3, 5, 7, 9, 10, 12, 14, 16 and 18. Input: L = 128, R = 162Output: 7Explanation: The numbers sa
15+ min read
C++ Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N
Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples:  Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6, 3, 8, 5, 2} Explanation: Even element = {2, 4, 6
2 min read
Java Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N
Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples:  Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6, 3, 8, 5, 2} Explanation: Even element = {2, 4, 6
2 min read
Python3 Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N
Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples:  Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6, 3, 8, 5, 2} Explanation: Even element = {2, 4, 6
2 min read
Javascript Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N
Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples:  Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6, 3, 8, 5, 2} Explanation: Even element = {2, 4, 6
2 min read
Python program to print all negative numbers in a range
Given the start and end of a range, write a Python program to print all negative numbers in a given range. Examples: Input: a = -4, b = 5 Output: -4, -3, -2, -1 Input: a = -3, b= 4 Output: -3, -2, -1 Method: Print all the negative numbers using a single-line solution. Print all negative numbers using for loop. Define the start and end limits of the
4 min read
Python program to print all even numbers in a range
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
5 min read
Python program to print all positive numbers in a range
Given start and end of a range, write a Python program to print all positive numbers in given range. Example: Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 Input: start = -3, end = 4 Output: 0, 1, 2, 3, 4 Example #1: Print all positive numbers from given list using for loop Define start and end limit of range. Iterate from start till the rang
4 min read