Print First 100 Odd Numbers in Python

Print First 100 Odd Numbers in Python | This article will provide you with different solutions for finding out the first 100 odd numbers in python. We will make use of for loop, while loop, and if block. 

If you look attentively, you’ll notice that we initiated the range at 1 and utilized the counter value of 2. It means that the first number of iterations is 1, the second is 3 (but 2), etc.

Program to Print First 100 Odd Numbers in Python Using the for loop

number = 100

for num in range(1, number + 1, 2):
   print(num, end=" ")

Output:-

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

In the above program, we have used end=” “ to display the numbers without newlines. If you want to print numbers in the different lines then you can directly use print(num).

We have started from 1 and printed the number, in each iteration we have increased the value by 2. The iteration continues till the number becomes 100.

Let us see the same above program by taking input from the user. We will ask the user to enter the max value of the range.

max = int(input("Enter the max value of the range: "))

for num in range(1, max + 1, 2):
   print(num, end=" ")

Output:-

Enter the max value of the range: 10
1 3 5 7 9

Enter the max value of the range: 100
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

Python for Loop ensures that the odd numbers are around 1 and 100 in this case. In the previous program we increase the number by 2. But in this program we will increase the number by 1, and will check whether the given number is odd number or not.

max_num = 100

for number in range(1, max_num + 1):
   if(number % 2 != 0):
      print(number, end=" ")

Output:-

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

The below program takes input from the end-user and finds the odd numbers between 1 to the given number.

Print first 100 odd numbers in Python using if statement by taking input from the end-user

max_num = int(input("Enter the max value: "))

for number in range(1, max_num + 1):
   if(number % 2 != 0):
      print(number, end=" ")

Output:-

Enter the max value: 20
1 3 5 7 9 11 13 15 17 19

Enter the max value: 100
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

We effectively replaced the for loop with the while loop in this Python program.

max_num = 100

number = 1

while number <= max_num:
   if(number % 2 != 0):
      print(number, end=" ")
   number = number + 1

Output:-

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

The below program takes input from the end-user and display the odd numbers between 1 to the given number.

max_num = int(input("Enter the max value: "))

number = 1

while number <= max_num:
   if(number % 2 != 0):
      print(number, end=" ")
   number = number + 1

Output:-

Enter the max value: 50
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

Also see:- Python Iterate Over Digits in Integer

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *