Python Program to check whether the user input value is odd or even


May 2, 2022, Learn eTutorial
1300

To understand this example, you should have knowledge of the following Python programming topics:

How to check odd or even in the python program?

In this beginner-level python program, we want to check if a number is an even or odd number. To check a number is even or odd, we need to know an even number and an odd number. Any number greater than zero, which is divisible by 2, is an Even number. For example, 6 is even because it is divisible by 2. Any number which is not divisible by 2 is an Odd number. For example, 3 is not divisible by 2, which is an odd number. In this python program, we have to implement this logic using the if condition in python.

After accepting the number from the user using the input method in python language. We save the number in a variable after converting the input string to an integer using int in python. Then we use a Mod Operator with a given number by 2 and check the remainder is zero or not. Using if condition checks if the remainder is zero, then using the print function, display a number is an even number. Else Display is an Odd number.

ALGORITHM

STEP 1: Accept a string using the input function and convert it to an integer using int in python. Then store the value in a variable.

STEP 2: Use the mod operator to check the number is divisible by 2. Use an if condition to check the result and print the number is even if the condition is satisfied.

STEP 3: Use elif statement and use a mod operator to check the number is not divisible by 2 and print a number as an odd number.

STEP 4: Use a else statement and print the number is not a valid input.

Python Source Code

                                          num = int(input("Enter any number: "))

if (num % 2) == 0:   # check using mod operator and if that is true then print number is even

    print(num, "it is an even number")

elif (num % 2) == 1:  # Check using mod operator and if that not true then print number is odd

    print(num, "it is an odd number")  
else:

    print("Error, this is not a valid input")  # print the invalid input
                                      

OUTPUT

Enter any number : 6

6 it is a even number.