Odd Number in Python

Python program to check given number is an odd number or not? We will discuss different methods to check number is an odd number or not. A number will be given to the program and the python program will check the given number is an odd number or not. We will also find all odd numbers in between a given range in Python.

Odd number:- A number is called an odd number if it is not divisible by 2.

Example:- 1, 3, 5, 7 e.t.c. are odd numbers but 2, 4, 6, 8 are not an odd number because they are completely divisible by number 2, they are even numbers.

Check if the Number is Odd in Python

This is the simplest and easiest python program to check given number is an odd number or not. We will take one number while declaring the variables. Python program will check given number is an odd number or not using mathematical calculation and finally, it will be displayed on the screen.

Program description:- Write a Python program to check number is odd?

# Python program to check given number is an odd or not

# take inputs
num = 3

# check number is odd or not
if(num % 2 != 0):
 print('{0} is an odd number'.format(num))
else:
 print('{0} is not an odd number'.format(num))

Output:-

3 is an odd number

In this program, value for the number was hardcoded in the program.

num = 3

Then, checked the given number is an odd number or not using the if-else statement and finally, display the result.

if(num % 2 != 0):
 print('{0} is an odd number'.format(num))
else:
 print('{0} is not an odd number'.format(num))

Odd Numbers in Python

In the previous program, inputs were hardcoded in the program but in this program, inputs will be provided by the user.

# Python program to check given number is an odd or not

# take inputs
num = int(input('Enter a number: '))

# check number is odd or not
if(num % 2 != 0):
 print('{0} is an odd number'.format(num))
else:
 print('{0} is not an odd number'.format(num))

Output for the different input values:-

Enter a number: 1
1 is an odd number

Enter a number: 10
10 is not an odd number

Enter a number: 253
253 is an odd number

In this program, inputs are scanned using the input() function and stored in variable num.

num = int(input('Enter a number: '))

Then, check the given number is an odd number or not using the if-else statement and finally, display the result.

Using Functions

We can also take the help of a function to check number is odd or not in python. A function is a block of code that performs a specific task.

# Python program to check given number is an odd or not using function

# Returns true if num is odd, else not odd 
def isOdd(num):
 # check number is odd or not
 return (num % 2 != 0)

#take inputs
num = int(input('Enter a number: '))

# display result
if isOdd(num):
 print(num,"is an odd number")
else:
 print(num,"is not an odd number")

Output for the different input values:-

Enter a number: 15
15 is an odd number

Enter a number: 20
20 is not an odd number

In this program, first of all a function isOdd() is defined which checks the passed value is an odd number or not?

# Returns true if num is odd, else not odd 
def isOdd(num):
 # check number is odd or not
 return (num % 2 != 0)

Inputs are scanned using the input() function and stored in variable num. Then call the function and print the result condition.

Find Odd Numbers in the Range

In this program, We will learn how to print odd numbers in the given range. Example:- Python program will be print all odd numbers in the 1 to 10 number range.

# Python program to print all odd numbers in given range 

# take range
start = int(input('Start: '))
end = int(input('End: '))

print('All odd number in the range')

for num in range(start, end + 1):
    # check number is odd or not
    if num % 2 != 0:
        print(num, end = " ")

Output for the different input values:-

Start: 1
End: 10
All odd number in the range
1 3 5 7 9

Start: 5
End: 25
All odd number in the range
5 7 9 11 13 15 17 19 21 23 25

Program to Print Odd Number from 1 to 100

# Python program to print all odd numbers in given range 

# take range
start, end = 1, 100

print('All odd number in the range')

for num in range(start, end + 1):
    # check number is odd or not
    if num % 2 != 0:
        print(num, end = " ")

Output:-

All odd number in the range
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

Also See:- Even Number Python Program

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

Leave a Reply