Hi, in this blog let's try to learn how to write a Python program to check whether a number is even or odd.
A number is said to be an even number if it is completely modulated by 2. A number is said to be an odd number if it is giving a reminder of 1 when we modulate by 2
let's consider an example
Let's take two numbers and try to modulate it by 2
number = 18
18 % 2 we get Reminder as 0
So 18 is even number
number = 25
25 % 2 we get Reminder as 1
So 25 is an odd number
Below is the program to check if the number is odd or even
number = int(input("Enter a Number : "))
if number % 2 == 0:
print("The Number is Even Number")
else:
print("The Number is Odd Number")
Output:
Enter a Number : 3
The Number is Odd Number
Output:
Enter a Number : 32
The Number is Even Number
Output:
Enter a Number : 355
The Number is Even Number
You can also write this Python program using the Bitwise or operator as show below
When you try to use bitwise and operator with 1 so it will perform an operation on all the bits, if it is an odd Number the output will be 1 so let's use this with python if statement and try to check if the number is odd or not.
number = int(input("Enter a Number : "))
if (number & 1):
print("The Number is Odd Number")
else:
print("The Number is Even Number")
Output:
Enter a Number : 3
The Number is Odd Number
Output:
Enter a Number : 22
The Number is Even Number
Output:
Enter a Number : 25
The Number is Even Number
python program to check even or odd using function
let's use of modulus with function to check if the number is odd or even
def check_even(temp_number):
if temp_number % 2 == 0:
return True
number = int(input("Enter a Number : "))
if (check_even(number)):
print("The Number is Even Number")
else:
print("The Number is Odd Number")
OUTPUT:
Enter a Number : 22
The Number is Even Number
OUTPUT:
Enter a Number : 55
The Number is Odd Number
Confusion: Try to run the program by yourself and check if number is odd or not
Keywords: Write a Program to Check Whether a Number is Even or Odd, python program to separate even and odd numbers, python program to segregate even and odd numbers, python program for sum of even and odd numbers, python program to check even odd, python program to print even odd, python program to odd or even,python program to check even or odd using function, python program for even odd using function, python program to check even or odd using function, python program to find if number is even or odd using recursion, python program to find odd or even using function, even odd program in python using if else, even odd program in python without using modulus, python program even and odd number, python program for even and odd numbers, python program for even and odd, python program to find even odd and prime numbers from the given array, python program to check leap year/odd-even number
0 Comments