Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Even Odd Program in Python (How to Check Number is Even or Odd?)
Are you tired of manually checking whether a number is odd or even
Let’s explore various ways to tackle this common programming task using Python. From traditional approaches using for loops and while loops to more concise solutions leveraging functions, lambda, arrays, and even the mighty ternary operator, we've got you covered.
We'll even delve into creative techniques that don't rely on the modulus operator. Whether you're a beginner or an experienced Pythonista, this guide will equip you with multiple ways to check if a number is odd or even in Python.
Odd or Even Program in Python
Code
# How to check if number is odd or even
number = int(input("Enter a number: "))
if number % 2 == 0:
print("The number is even")
else:
print("The number is odd")
Output
Enter a number: 5
The number is odd
Explanation
In this program, we prompt the user to enter a number using the input() function. The input is converted to an integer using the int() function and stored in the variable number.
We use the modulo operator % to check if the number is divisible by 2. If the remainder of the division is 0, it means the number is even. If the remainder is not 0, it means the number is odd.
-
If the number is divisible by 2 (i.e., the remainder is 0), we print "The number is even".
-
If the number is not divisible by 2 (i.e., the remainder is not 0), we print "The number is odd".
When you run this program, it will prompt you to enter a number. After you provide the input, it will check if the number is even or odd and display the corresponding message.
Even Odd Program in Python Using Function
Code
# Python program with function to check if a number is even or odd
def check_even_odd(number):
if number % 2 == 0:
return "even"
else:
return "odd"
# Get input from the user
number = int(input("Enter a number: "))
# Call the function and print the result
result = check_even_odd(number)
print("The number is", result)
Output
Enter a number: 99
The number is odd
Explanation
Here, we have defined the check_even_odd function that takes a number as an argument. The function uses the modulo operator % to check if the number is divisible by 2. If the remainder of the division is 0, it means the number is even. Otherwise, it is odd.
The function returns the string "even" if the number is even, and "odd" if the number is odd.
We prompt the user to enter a number using the input() function, convert it to an integer using the int() function, and store it in the variable number.
We then call the check_even_odd function, passing in the number as an argument, and store the result in the variable result.
Finally, we print the result, indicating whether the number is even or odd.
Odd or Even Program in Python Using for loop
If you want to check multiple numbers and check if they are odd or even using a for loop in Python, you can use this program:
Code
# Using for loop to check even odd number
def check_even_odd(number):
if number % 2 == 0:
return "even"
else:
return "odd"
# Get input from the user
count = int(input("How many numbers do you want to check? "))
# Iterate for each number
for i in range(count):
num = int(input("Enter number {}: ".format(i+1)))
result = check_even_odd(num)
print("Number {} is {}".format(num, result))
Output
How many numbers do you want to check? 5
Enter number 1: 9
Number 9 is odd
Enter number 2: 3
Number 3 is odd
Enter number 3: 0
Number 0 is even
Enter number 4: 11
Number 11 is odd
Enter number 5: 565
Number 565 is odd
Explanation
We definied the check_even_odd function. It takes a number as an argument and returns the string "even" if the number is even, or "odd" if it's odd.
Next, we prompt the user to enter the number of values they want to check using the input() function. The input is converted to an integer using the int() function and stored in the variable count.
We then use a for loop that iterates count number of times. Within each iteration, we prompt the user to enter a number and store it in the variable num.
We call the check_even_odd function, passing num as an argument, and store the result in the variable result.
Finally, we print the number entered and its corresponding result, indicating whether it is odd or even.
Odd or Even Program in Python Using while loop
Code
# Check multiple numbers, whether even or odd using while loop
def check_even_odd(number):
if number % 2 == 0:
return "even"
else:
return "odd"
# Get input from the user
count = int(input("How many numbers do you want to check? "))
# Initialize a counter
i = 0
# Iterate until counter reaches the count
while i < count:
num = int(input("Enter number {}: ".format(i+1)))
result = check_even_odd(num)
print("Number {} is {}".format(num, result))
i += 1
Output
How many numbers do you want to check? 6
Enter number 1: 82
Number 82 is even
Enter number 2: 44
Number 44 is even
Enter number 3: 26
Number 26 is even
Enter number 4: 7
Number 7 is odd
Enter number 5: 1
Number 1 is odd
Enter number 6: 9
Number 9 is odd
Explanation
We defined the check_even_odd function. It takes a number as an argument and returns the string "even" if the number is even, or "odd" if it's odd.
Next, we prompt the user to enter the number of values they want to check using the input() function. The input is converted to an integer using the int() function and stored in the variable count.
We then initialize a counter i to keep track of the number of iterations.
Using a while loop, we iterate until the counter i reaches the count value specified by the user. Within each iteration, we prompt the user to enter a number and store it in the variable num.
We call the check_even_odd function, passing num as an argument, and store the result in the variable result.
Finally, we print the number entered and its corresponding result, indicating whether it is odd or even.
Even Odd Program in Python Using List
Code
# Check if numbers in a list are even or odd
def check_even_odd(number):
if number % 2 == 0:
return "even"
else:
return "odd"
# Get input from the user
numbers = input("Enter a list of numbers separated by spaces: ").split()
# Convert the input into a list of integers
numbers = [int(num) for num in numbers]
# Check if each number is even or odd
for num in numbers:
result = check_even_odd(num)
print("Number {} is {}".format(num, result))
Output
Enter a list of numbers separated by spaces: 4 8 23 83 102 47
Number 4 is even
Number 8 is even
Number 23 is odd
Number 83 is odd
Number 102 is even
Number 47 is odd
Explanation
We define the check_even_odd function.
Next, we prompt the user to enter a list of numbers separated by spaces using the input() function. The input is split into individual numbers using the split() method, and the resulting list of numbers is stored in the variable numbers.
We then use a list comprehension to convert each number in the numbers list from a string to an integer.
Finally, we iterate over each number in the numbers list using a for loop. Within each iteration, we call the check_even_odd function to determine whether the number is odd or even, and print the corresponding result.
Even Odd Program in Python Using Lambda
Code
# Using lambda function to check even or odd number
# Get input from the user
numbers = input("Enter a list of numbers separated by spaces: ").split()
# Convert the input into a list of integers
numbers = [int(num) for num in numbers]
# Check if each number is even or odd using lambda function
results = list(map(lambda num: "even" if num % 2 == 0 else "odd", numbers))
# Print the results
for num, result in zip(numbers, results):
print("Number {} is {}".format(num, result))
Output
Enter a list of numbers separated by spaces: 3 0 47 363 28 772
Number 3 is odd
Number 0 is even
Number 47 is odd
Number 363 is odd
Number 28 is even
Number 772 is even
Explanation
We start by prompting the user to enter a list of numbers separated by spaces using the input() function. The input is split into individual numbers using the split() method, and the resulting list of numbers is stored in the variable numbers.
We then use a list comprehension to convert each number in the numbers list from a string to an integer.
Next, we use the map() function with a lambda function to check if each number in the numbers list is even or odd. The lambda function takes a number as input and returns the string "even" if the number is divisible by 2, and "odd" otherwise. The results are stored in the results list.
Finally, we iterate over each number and result in parallel using the zip() function and print them.
Even Odd Program in Python Using Array
Code
# Using NumPy to find even odd using array
import numpy as np
def check_even_odd(number):
if number % 2 == 0:
return "even"
else:
return "odd"
# Get input from the user
numbers = input("Enter a list of numbers separated by spaces: ").split()
# Convert the input into a numpy array of integers
numbers = np.array([int(num) for num in numbers])
# Check if each number is even or odd using a vectorized lambda function
results = np.vectorize(lambda num: "even" if num % 2 == 0 else "odd")(numbers)
# Print the results
for num, result in zip(numbers, results):
print("Number {} is {}".format(num, result))
Output
Enter a list of numbers separated by spaces: 2 77 29 22
Number 2 is even
Number 77 is odd
Number 29 is odd
Number 22 is even
Explanation
In this program, we start by importing the numpy library as np. It provides efficient array operations.
We prompt the user to enter a list of numbers separated by spaces using the input() function. The input is split into individual numbers using the split() method, and the resulting list of numbers is stored in the variable numbers.
We then use a list comprehension to convert each number in the numbers list from a string to an integer. The resulting list is converted into a numpy array using np.array().
Next, we define the check_even_odd function.
To check if each number in the numbers array is even or odd, we use a vectorized lambda function with np.vectorize(). The lambda function takes a number as input and returns the string "even" if the number is divisible by 2, and "odd" otherwise. The vectorized lambda function is applied to the numbers array, and the results are stored in the results array.
Finally, we iterate over each number and result in parallel using the zip() function and print them.
Odd Even Using Ternary Operator in Python
Code
# Get input from the user
number = int(input("Enter a number: "))
# Check if the number is odd or even using ternary operator
result = "even" if number % 2 == 0 else "odd"
# Print the result
print("The number is", result)
Output
Enter a number: 55
The number is odd
Explanation
We start by prompting the user to enter a number using the input() function. The input is converted to an integer using the int() function and stored in the variable number.
Next, we use the ternary operator to check if the number is even or odd. The condition number % 2 == 0 checks if the number is divisible by 2 (i.e., even). If the condition is true, the value "even" is assigned to the variable result; otherwise, the value "odd" is assigned.
Finally, we print the result, indicating whether the number is odd or even.
Even Odd Program in Python Without Using Modulus
Code
# Check if a number is odd or even without using the modulus operator
# Get input from the user
number = int(input("Enter a number: "))
# Check if the number is odd or even without using modulus
result = "even" if (number // 2) * 2 == number else "odd"
# Print the result
print("The number is", result)
Output
Enter a number: 66
The number is even
Explanation
We prompt the user to enter a number using the input() function. The input is converted to an integer using the int() function and stored in the variable number.
To check if the number is odd or even without using the modulus operator, we divide the number by 2 using the floor division operator // to get the integer division. We then multiply the result by 2 and compare it with the original number. If the two values are equal, it means the number is even; otherwise, it's odd.
We use the ternary operator to assign the appropriate result ("even" or "odd") to the variable result.
Finally, we print the result, indicating whether the number is odd or even.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program