CREATE A FUNCTION TO CHECK EVEN OR ODD IN PYTHON

In this topic, we will discuss how to easily and interestingly to create a function that check whether a number is even or odd in Python programming.  Let’s explore the methods to check the given number is odd or even.

To Check Given Number Is Even Or Odd

 

Table of Contents:

  • Introduction
  • Program to check given number is even or odd
  • Output
  • Conclusion

Introduction:

First of all, we will create function using ‘if-else’ condition .  The function will return “Even”  if the given number is even and “Odd”  if the given number is odd.

Program:

 

def even_or_odd(number):
    if number % 2 == 0:
        print(number," is Even")
    else:
        print(number,"is Odd")

Function Explanation:
  • This method , we use the module operator(%) to check the number is even or odd in Python.
  • The % operator calculates the remainder of the division of givennumber by 2.
  • If the remainder is 0, the given number is even, and the function returns "number is even".
  • Otherwise, the function returns "number is odd".

We can use this function by passing any  integer within an argument.  It will find out if the given number is even or odd and retrieve the appropriate string.

number=int(input("Enter some integer to be check:"))
print(even_or_odd(number))

Output:

Enter some integer to be check:5
5 is Odd

 

Conclusion:

In this conclusion, Python programming offers us to flexible modules and methods to check the given number is odd or even.  Python control statements such as if-else, while loop, for loop etc,. is used to check these kind of programs.

Thank you for visiting our site!!!!….

 

Leave a Comment

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

Scroll to Top