In this post, we will see how to print first n odd natural numbers in python?

**Odd Numbers:**

Any integer that is positive or negative which can not be divided exactly by 2 is an odd number. The numbers having the last digit is 1, 3, 5, 7 or 9 are odd numbers.

**Note:** Natural number is positive integers. It’s range from 1 to infinity.

**Examples:**

Input:n: 5

Output:5 first odd natural numbers are: 1 3 5 7 9

Input:n: 6

Output:6 first odd natural numbers are: 1 3 5 7 9 11

Let’s see the different version of codes for this program :

**Code version 1:** Using a counter.

In this code, we take a counter variable ‘odd’ initialized by 1 and on every iteration, this counter value is incremented by 2.

# main code if __name__ == "__main__" : # input value of n n = 11 # take a odd variable # and assign 1 to it odd = 1 print("odd numbers are: ") # loop run from 0 # to n - 1 for i in range(n) : # print value of odd variable # and end parameter is ',' # so print all outputs # in a single line with ',' print(odd, end = ",") # increment by 2 # in every iteration odd += 2

Output:odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,

**Code version 2:** Using an if-else statement along with for loop.

In this code, we run a loop from 1 to 2 * n – 1, and in every iteration, we check the value of i is divisible by 2 or not if not divisible then it is odd otherwise even.

# main code if __name__ == "__main__" : # input value of n n = 7 print("odd numbers are: ") # loop run from 1 # to 2 * n - 1 for i in range(1, 2 * n) : # if condition is True # that means i % 2 gives # non-zero value then if i % 2 : # print value of i variable # and end parameter is ',' # so print all outputs # in a single line with ',' print(i, end = ",")

Output:odd numbers are: 1,3,5,7,9,11,13,

**Note** : Code version 1 and 2 can also be written using While loop.

**Code version 3:** Using a range() function concept.

As we know, we can pass 3 parameters in range() function, the third parameter is stepped i.e jump after each iteration.

# main code if __name__ == "__main__" : # input value of n n = 7 print("odd numbers are: ") # loop run from 1 # to 2 * n - 1 for even in range(1, 2 * n, 2) : # print value of odd variable # and end parameter is ',' # so print all outputs # in a single line with ',' print(odd, end = ",")

Output:odd numbers are: 1,3,5,7,9,11,13,