Python Program to Print All Odd Numbers in a Range

This is a Python Program to print odd numbers within a given range.

Problem Description

The program takes the upper and lower limit and prints all odd numbers within a given range.

Problem Solution

1. Take in the upper range limit and the lower range limit and store it in separate variables.
2. Use a for-loop ranging from the lower range to the upper range limit.
3. Then use an if statement if check whether the number is odd or not and print the number.
4. Exit.

Program/Source Code

Here is the source code of the Python Programto print odd numbers within a given range. The program output is also shown below.

 
lower=int(input("Enter the lower limit for the range:"))
upper=int(input("Enter the upper limit for the range:"))
for i in range(lower,upper+1):
    if(i%2!=0):
        print(i)
Program Explanation

1. User must enter the upper range limit and the lower range limit.
2. The for loop ranges from the lower range limit to the upper range limit.
3. The expression within the if-statement checks if the remainder obtained when the number divided by 2 is one or not (using the % operator).
4. If the remainder isn’t equal to 0, the number is odd and hence the number is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter the lower limit for the range:1
Enter the upper limit for the range:16
1
3
5
7
9
11
13
15
 
Case 2:
Enter the lower limit for the range:150
Enter the upper limit for the range:180
151
153
155
157
159
161
163
165
167
169
171
173
175
177
179

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.