ScanSkill

Check Even or Odd Number in Python

In this, you'll get to learn how to check even or odd number in Python. And the number is input from the user. i.e. Let's say if the user entered 4, then the output should be “it is an Even number”.

This covers four different approaches:

  • Check even or odd number in Python using if-else
  • Check even or odd number in Python using the function
  • Check even or odd number in Python using class

Prerequisites

Examples

Check even or odd number in Python using if-else

This is an example to check whether a number entered by the user is an even number or odd.

print("Enter the Number: ")
num = int(input())
if num%2==0:
    print("{num} is an Even Number")
else:
    print("{num} is an Odd Number")

Here, the f-string is a string literal that is used as an f at the beginning and curly braces containing expression that will be replaced with its value.

And, % (modulus) operator returns the decimal part(remainder) of the quotient.

Output:

Enter the Number: 
5
5 is an Odd Number
Enter the Number: 
4
4 is an Even Number

Check even or odd number in Python using the function

This example is to check whether a number entered by the user is an even number or odd.

def checkEvenOdd(n): 
    if n % 2 == 0:
        print(f"{n} is an Even Number")
    else:
        print(f"{n} is an Odd Number")

print("Enter the Number: ")
num = int(input())

checkEvenOdd(num)

Output:

Enter the Number: 
5
5 is an Odd Number
Enter the Number: 
4
4 is an Even Number

Check even or odd number in Python using Class

This also does the same task as the above programs but using the Class method.

class EvenOdd:
    def checkEvenOdd(self, n): 
        if n % 2 == 0:
            print(f"{n} is an Even Number")
        else:
            print(f"{n} is an Odd Number")

print("Enter the Number: ")
num = int(input())

obj1 = EvenOdd()
res = obj1.checkEvenOdd(num)

Here, all properties of the class named EvenOdd get assigned to an object named obj1. Then the object can be used to access the member function of the class checkEvenOdd using the dot (.) operator. (obj1.checkEvenOdd(num))

Output:

Enter the Number: 
5
5 is an Odd Number
Enter the Number: 
4
4 is an Even Number

Conclusion

In this, we discussed different examples for different methods to find the average of n numbers in Python -- using if-else, using a function, and using class.