Python Program to Check if a Number is Odd or Even
In this post, you will learn how to write different programs in Python to check whether the given number is even or odd. To check whether a number is odd or even, you can either ask a user to enter a number or you can provide a list from which the program can check whether the number is odd or even. Such a type of logical question is generally asked in an interview or in competitive exams. This may be helpful in improving your logical programming skills.
A number is even if it is perfectly divisible by 2, and if the remainder is not zero, the number is odd.
Even odd program in Python using for loop
In the given Python program, we have used the for loop to check and count the even and odd numbers in a list.
# Check and count Odd and Even numbers in a list
# list of numbers
numbers = [11, 30, 15, 41, 22, 85, 7]
even_count, odd_count = 0, 0
# iterating list
for num in numbers:
if num % 2 == 0:
even_count += 1
print("Even Number: ", num)
else:
odd_count += 1
print("Odd Number: ", num)
print("Total Even numbers in the list: ", even_count)
print("Total Odd numbers in the list: ", odd_count)
When we execute the above code, it returns the following output-
Odd Number: 11
Even Number: 30
Odd Number: 15
Odd Number: 41
Even Number: 22
Odd Number: 85
Odd Number: 7
Total Even numbers in the list: 2
Total Odd numbers in the list: 5
Even odd program in Python using user input
Here, we check whether num is even or odd by using the if...else statement in Python. We calculate its remainder using the % operator and check if it is divisible by 2 or not. If the num is divisible by 2, we print that the num is even. Otherwise, we print the num is odd.
# Check Odd and Even number from user input
# take user input
num = input("Enter any number: ");
try:
num = float(num);
except ValueError:
print("Please enter a number...");
if num % 2 == 0:
print(int(num), "is an even number.");
else:
print(int(num), "is an odd number.");
When you execute the above program, it will return something like this-
Enter any number: 79
79 is an odd number.
Enter any number: 89
89 is an odd number.
Enter any number: 42
42 is an even number.
Even odd program in Python using While loop
In the given Python program, we have used the while loop to check and count the odd and even numbers in a list.
# Check and count Odd and Even numbers in a list
# list of numbers
numberlist = [11, 30, 15, 41, 22, 85, 7]
num = 0
even_count, odd_count = 0, 0
# iterating list
while(num < len(numberlist)):
if numberlist[num] % 2 == 0:
even_count += 1
print("Even Number: ", numberlist[num])
else:
odd_count += 1
print("Odd Number: ", numberlist[num])
num +=1
print("Total Even numbers in the list: ", even_count)
print("Total Odd numbers in the list: ", odd_count)
Odd Number: 11
Even Number: 30
Odd Number: 15
Odd Number: 41
Even Number: 22
Odd Number: 85
Odd Number: 7
Total Even numbers in the list: 2
Total Odd numbers in the list: 5
Even odd program in Python using Lambda Function
A lambda function is a small anonymous function that takes any number of arguments, but can only have one expression. Here, we have used the lambda function to check for even and odd numbers.
# Check and count Odd and Even numbers using Lambda function
# list of numbers
numberlist = [89, 43, 44, 29, 78, 22]
# print Even numbers in a list using Lambda function
even_numbers = list(filter(lambda x: x % 2 == 0,numberlist))
print("Total Even numbers in the list: ", even_numbers)
# print Odd numbers in a list using Lambda function
odd_numbers = list(filter(lambda x: x % 2 == 1,numberlist))
print("Total Odd numbers in the list: ", odd_numbers)
Output of the above code:
Total Even numbers in the list: [44, 78, 22]
Total Odd numbers in the list: [89, 43, 29]
Related Articles
Multiply all elements in list PythonInsert XML Data to MySQL Table using Python
Python program to map two lists into a dictionary
Python program to multiply two numbers
Remove last element from list Python
How to find the stop words in nltk Python
Python Spell Checker Program
Python remove punctuation from string
How to convert Excel to CSV Python Pandas
How to read data from excel file using Python Pandas
How to read data from excel file in Python
Python read JSON from URL requests
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml