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

 

 

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 to 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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago