Python program to print all odd numbers in a range
In this tutorial, we will learn to write a program that will print all the odd numbers in a range. Odd numbers are the numbers that are not divisible by 2. The user has to input the upper limit and the lower limit of the range. Then we have to find all the odd numbers in that range and display them. We will be using the concept of loops in Python and conditional statement in Python in our program.
For a given list of numbers, the task is to find and print all the odd numbers in that range.
Input: lower limit= 4
upper limit= 10
Output: 5 7 9
Input: lower limit= 7
upper limit= 37
Output: 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35
Approach to print all odd numbers in a range
For executing this task, we will use a for loop which will run from the lower limit to the upper limit. We will check all the numbers in that range if they are not divisible by 2. Numbers that satisfy the condition will be printed.
Follow the algorithm to understand the approach better.
Step 1- Take input of lower limit of the range
Step 2- Take input of upper limit of the range
Step 3- Run a loop from the lower limit to the upper limit
Step 4- Check for each number in the range is not divisible by 2
Step 5- If yes, print the number
Look at the program to understand the implementation of the above-mentioned approach.
#print odd numbers
ll=int(input("Enter lower limit "))
ul=int(input("Enter upper limit "))
print("odd numbers in the range are")
for i in range(ll,ul):
Enter lower limit 6
Enter upper limit 29
odd numbers in the range are
7 9 11 13 15 17 19 21 23 25 27
For including both the limits in the range we will run the loop from the lower limit to (upper limit+1)
To print the numbers with space we have used end=" "
In this tutorial, we have learned how to find and print all the odd numbers in a range. We have used for loop and if conditional statement for checking and finding all the odd numbers in the range.