Python Program to Print Odd Numbers from 1 to N

Write a Python Program to Print Odd Numbers from 1 to N using While Loop and For Loop with an example.

Python Program to Print Odd Numbers from 1 to N using For Loop

This Python program allows the user to enter the maximum limit value. Next, it is going to print odd numbers from 1 to the user entered a maximum limit value.

In this example, Python For Loop makes sure that the odd numbers are between 1 and maximum limit value.

TIP: I suggest you refer to Python Odd or Even Program article to understand the logic behind Python Odd numbers.

maximum = int(input(" Please Enter any Maximum Value : "))

for number in range(1, maximum + 1):
if(number % 2 != 0):
print("{0}".format(number))

The odd numbers using for Loop and if statement output

 Please Enter any Maximum Value : 10
1
3
5
7
9

for loop without If statement

This program for Odd Numbers from 1 to N code is the same as above. But, we altered the For Loop to eliminate If block.

If you observe closely, we started the range from 1, and we used the counter value is 2. It means, for the first iteration number = 1, second iteration number = 3 (not 2) so on.

maximum = int(input(" Please Enter any Maximum Value : "))

for number in range(1, maximum + 1, 2):
print("{0}".format(number))

Printing the odd numbers using for Loop output.

 Please Enter any Maximum Value : 12
1
3
5
7
9
11

Python Program to Print Odd Numbers using While Loop

In this odd numbers program, we just replaced the For Loop with While Loop.

maximum = int(input(" Please Enter the Maximum Value : "))

number = 1

while number <= maximum:
if(number % 2 != 0):
print("{0}".format(number))
number = number + 1
 Please Enter the Maximum Value : 15
1
3
5
7
9
11
13
15

Python Program to display Odd Numbers from 1 to 100 using For Loop

This display odd numbers program allows users to enter Minimum and maximum value. Next, it displays odd numbers between Minimum and maximum value.

minimum = int(input(" Please Enter the Minimum Value : "))
maximum = int(input(" Please Enter the Maximum Value : "))

for number in range(minimum, maximum+1):
if(number % 2 != 0):
print("{0}".format(number))
Python Program to Print Odd Numbers from 1 to N