Python - Find the Number is Odd or Even

In this program, you will learn how to check the given number is odd or even number.


You can check number by dividing it with number 2. If the number is divisible by 2 with remainder zero, then the number is Even. If the number is divided by 2 with remainder non zero value, then the number is Odd. You can use remainder operator % to compute the remainder.

Program Code



    # This program to check the given is Odd or Even number

    # The number is exactly divisible by 2, it is an even number. 
    Example: 0,2,4,6,8,...etc
    # The number is not exactly divisible by 2, it is a Odd number. 
    Example: 1,3,5,7,9,...etc
    
    #num = 50
    num = int(input('Enter the number: '))
    
    # check the number is divisible by 2
    if (num % 2==0): 
        print('The number {0} is a even number'.format(num))
    
    # check the number is not divisible by 2
    elif (num % 2 != 0):
         print('The number {0} is a odd number'.format(num))
    
    # otherwise, it is not a valid number
    else:
        print('The input is not valid')

Output 1



    Enter the number: 5
    The number 5 is a odd number

Output 2



    Enter the number: 10
    The number 10 is a even number
    

In this program, the variable num is used to holds the given number.

First, check the number num is divisible by 2.
Then, check the number num is not divisible by 2.
Then, consider the given number num is not a valid number.



The following topics can be useful to understand this program.



Related Tutorials