Python program to print all odd numbers in a range
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
- Define the start and end limit of the 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 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 = " " ) |
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 = " " ) |
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) |
[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) |
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) |
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 ]) |
[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 = " " ) |
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) |
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. |
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 = " " ) |
5 7 9 11 13 15 17 19
Time Complexity: O(n)
Auxiliary Space: O(1)
Please Login to comment...