4 ways to check whether the given integer is Even or Odd in Python
- Home
- Check value
- 4 ways to check whether the given integer is Even or Odd in Python
- On
- By
- 0 Comment
- Categories: Check value
4 ways to check whether the given integer is Even or Odd in Python
4 ways to check whether the given integer is Even or Odd in Python
In this tutorial, we will discuss the concept of Python code for 4 ways to check whether the given integer is Even or Odd
In this post, we are going to learn how to check whether the given integer is even or odd using 4 different ways in the Python programming language.
Using modular operator
Program 1
#Python program to cehck if the input number is odd or even #Using modular operator num=int(input("Enter a number: ")) #Takes input from the user if(num%2)==0: #using modular operator print("{0} is even number".format(num)) else: print("{0} is odd number".format(num))
When the above code is executed, it produces the following results
Case 1
Enter a number: 234 234 is even number
Case 2
Enter a number: 111 111 is odd number
This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using modular operator
Using division operator
Program 2
#Python program to cehck if the input number is odd or even #Using division operator num=int(input("Enter a number: ")) #Takes input from the user res=int(num/2)*2;#division operator if(res==num): print("This number is even") else: print("This number is odd")
When the above code is executed, it produces the following results
Case 1
Enter a number: 514 This number is even
Case 2
Enter a number: 133 This number is odd
This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using division operator
Using bitwise operstor
#Python program to cehck if the input number is odd or even #Using division operator num=int(input("Enter a number: ")) #Takes input from the user if(num&1==1): #bitwise AND operator print(num," is a odd number") else: print(num," is a even number")
When the above code is executed, it produces the following results
Case 1
Enter a number: 401 401 is a odd number
Case 2
Enter a number: 998 998 is a even number
This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using bitwise operator
Using shift operator
#Python program to cehck if the input number is odd or even #Using shift operator num=int(input("Enter a number")) if(((num>>1)<<1)==num): #Using left shift and right shift operator print(num," is even number") else: print(num," is odd number")
When the above code is executed, it produces the following results
Case 1
Enter a number13 13 is odd number
Case 2
Enter a number24 24 is even number
This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using shift operator
Suggested for you
Similar post
C code to 5 ways to check whether the given integer is Even or Odd
Java code to 5 ways to check whether the given integer is Even or Odd
C++ code to 5 ways to check whether the given integer is Even or Odd
Python code to 5 ways to check whether the given integer is Even or Odd
Program to find whether a Number is Prime or Not in C++
Program to find whether a Number is Prime or Not in C
Program to find whether a Number is Prime or Not in Java
Program to find whether a Number is Prime or Not in Python
C++ program to find first n prime numbers
Java program to find first n prime numbers
C program to find first n prime numbers