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.

Check odd and even using operator

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

Python operators

Python datatype

If statement in Python

 

 

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

 

 

 

 

C++ code to 5 ways to check whether the given integer is Even or Odd
Example for Python program to check Prime number
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

1 day ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago