Python check if number is even

How to Check if a Number is Odd or Even using Python?

Python’s modulo (%) operator (also called the remainder operator) is useful to determine if a number is odd or even. We obtain the remainder of the division of a number by 2. If it is 0, it is even otherwise it is odd

Even Number − A number that can be divided by 2, leaving no remainders(remainder=0).

Odd Number − An odd number is one that cannot be divided by 2, so the remainder is 1.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input number.
  • Use the if conditional statement to check whether the input number modulus 2 is equal to 0 using the modulus (%)operator(returns the remainder).
  • Print even if the condition is true i.e, the remainder is 0.
  • Else Print odd if the condition is false i.e, the remainder is not 0.

Example

The following program returns the whether the input number is an even or an odd number using the modulo (%) operator −

# input number inputNumber = 10 # checking whether the number modulus 2 is equal to 0 if inputNumber%2==0: # printing even if the remainder is 0 print(inputNumber, "is an even number") else: # else printing odd print(inputNumber, "is an odd number")

Output

On executing, the above program will generate the following output −

Using Recursion

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a function checkEvenOdd to check whether the number passed to it as an argument is an even or odd number.
  • Use the if conditional statement to check whether the number is 0 and if it is 0 then the given number is even so return True.
  • Use another if conditional statement to check whether the number is 1 and if it is 1 then the given number is odd so return False.
  • Call the function again recursively by subtracting 2 from the given number.
  • Pass the input number as an argument to the checkEvenOdd() and call the function. Use the if conditional statement to check whether the function returns True or False.
  • Print even if the function returns True.
  • Else print odd if the function returns False
Читайте также:  Golang vs python benchmark

Example

The following program returns the whether the input number is an even or an odd number using the recursive function −

# creating a function that accepts a number as an argument and # checks whether it is an odd or even number def checkEvenOdd(num): # checking whether the number is 0 if(num==0): # returning True, if the number is even return True # checking whether the number is 1 elif(num==1): # returning False, if the number is odd return False else: # Calling the function again recursively by subtracting 2 from the given number return checkEvenOdd(num-2) # input number inputNumber= 7 # passing inuput number as an argument to the checkEvenOdd() and calling it if(checkEvenOdd(inputNumber)): # printing even if the function returns true print(inputNumber, "is an even number") else: # else printing odd if the function returns false print(inputNumber, "is an odd number")

Output

On executing, the above program will generate the following output −

Using Binary AND (&) operator

The idea is to check if the last bit of the number is set. If the last bit is set, the number is odd, otherwise it is even.

As you can see, bitwise ANDing(&) a number through a 1 gives a 1 if the number is odd because the last bit is already set. Otherwise, 0 is returned as output.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the if conditional statement to check whether the binary and(&) operation between the number passed and 1 is equal to 0 using the & operator.
  • If the condition is true, then the number is even and returns True.
  • Else the given number is odd so return False.
  • Create a variable to store the input number.
  • Pass the input number as an argument to the checkEvenOdd() and call the function. Use the if conditional statement to check whether the function returns True or False.
  • Print even if the function returns True.
  • Else print odd if the function returns False.

Example

The following program returns the whether the input number is an even or an odd number using the binary AND (&) operator −

# creating a function that accepts a number as an argument and # checks whether it is an odd or even number def checkEvenOdd(num): # checking whether num&1 == 0 if num & 1 == 0: # Then the number is even so return True return True # Else the number is odd # Then the number is odd so return False return False # input number inputNumber= 12 # passing input number as an argument to the checkEvenOdd() and calling it if(checkEvenOdd(inputNumber)): # printing even if the function returns true print(inputNumber, "is an even number") # else printing odd if the function returns false print(inputNumber, "is an odd number")

Output

On executing, the above program will generate the following output −

12 is an even number 12 is an odd number

Conclusion

In this article, we learned how to use three different methods to determine whether a given number is even or odd. We learned how to check the last bit of a given number using bitwise operators. We learned how to recursively call the function.

Читайте также:  Python file with syntax

Источник

Python How to Check If a Number Is Odd or Even (Examples)

An odd number is a number you cannot divide into two equal parts. An even number is a number you can evenly divide into two parts. In Python, you can use the modulo operator (%) to check if a number is odd or even.

n = 9 # 1. Check if a number is odd is_odd = n % 2 != 0 # 2. Check if a number is even is_even = n % 2 == 0

This guide teaches you how to check if a number is odd or even and how the modulo operator works in Python.

The Modulo Operator in Python

Modulo in python

In Python, the modulo operator (%) returns the remainder in the division between two numbers.

If this operation returns 0, it means a is evenly divisible by b.

For example, if you have 15 slices of pizza for 3 guests and you want to check if you can share the 15 slices with your guests evenly, you can use the modulo operator:

Make sure to check my complete guide to modulos in Python.

How to Use Modulo to Figure Out If a Number Is Odd or Even?

The modulo operator returns the remainder in the division.

  • An odd number isn’t evenly divisible by 2.
  • An even number is evenly divisible by 2.

In the context of modulos, this means:

  • Calculating modulo between an odd number and 2 returns 1 as a leftover value.
  • Calculating modulo between an even number and 2 returns 0.

How to Check If a Number Is Even in Python?

Using modulo to check if a value is even

You can use the modulo operator in Python to check if a number is even.

If you take a modulo between an even number and 2, the result is 0.

To write a program that checks if a number is even:

  1. Take the modulo between your target number and 2.
  2. Use a comparison operator to check if the result is equal to 0.

For example, let’s check if the number 9 is even:

number = 9 is_even = number % 2 == 0 print(is_even)

How to Check If a Number is Odd in Python

Using modulo to check if a value is odd

You can also use the modulo operator in Python to check if a number is odd.

If you take a modulo between an odd number and 2, the result is 1.

To write a program that checks if a number is odd:

  1. Take the modulo between your target number and 2.
  2. Use a comparison operator to check if the result is 1.

For instance, let’s see if the number 11 is odd:

number = 11 is_odd = number % 2 == 1 print(is_odd)

Example 1. Write a Function That Checks If a Number Is Odd/Even

In the previous examples, you learned how to use the modulo operator to check if a value is odd or even in Python.

But the above examples are just standalone code expressions. If you want to reuse the odd/even logic, you’d have to rewrite the comparisons over and over again.

To improve your code quality and readability, you can implement functions for checking if an input number is odd/even. This way you can reuse the code by calling the name of the function.

Читайте также:  Php get locale list

Here are two functions for calculating if a value is odd or even:

def is_even(n): return n % 2 == 0 def is_odd(n): return n % 2 != 0

Now you can use these functions anywhere and any number of times in your code.

print(is_odd(10)) print(is_even(6))

Example 2. Check If User Input Is Odd/Even

A common beginner-friendly Python example is to take user input and check if the input is odd/even.

Here’s a program that does it for you:

number = int(input("Enter a number: ")) if number % 2 == 0: print("The number is even") else: print("The number is odd")
Enter a number: 16 The number is even
  1. The input() function takes the user input in the console.
  2. The int() call converts the input to an integer from a string (text).
  3. The if-else statement checks if the input number is divisible by 2 or not.

Summary

Today you learned how to use Python to check if a number is odd or even.

To take home, you can use the modulo operator (%) to check for odd/even values. If the % operator returns 0 between a number and 2, the value is evenly divisible by 2 and is thus even. If the result is 1, the number is not divisible by 2, which means it’s odd.

Thanks for reading. Happy coding!

Read Also

Источник

How To Check If The Number Is Even Or Odd In Python

In this tutorial, learn how to check if the number is even or odd using Python. The short answer is to use the % (modulo) operator with the Python if conditional statement.

The even number is the multiple of 2 and the odd is not multiple of 2. Find the type of any number whether it is even or odd using method here. In the end, you will also find the other methods to find the number is even or odd in Python.

Check If The Number Is Even Using Python

An even number is a number which is perfectly divisible by 2 without any remainder. It divides the number by 2 and gets the remainder to check if equals to 0. If the remainder is zero(0), it confirms the number is even.

If you want to find the number is even or not, you have to use the % (modulo) operator.

The number is even number.

The above example finds the number which is even using the Python %(modulo) operator. You can change the above code as per your requirement to find the number if even.

Bonus: download a Free Python cheat sheet that will show you 20+ most important examples to learn in Python.

Find Out If the Given Number is Odd Using Python

An odd number is not a perfect divisible of 2 and gives some remainder number also. If the number divides by 2 and gives a remainder which does not equal to zero(0). Then, it is the odd number in Python. Below is the simple method you can use to find the number if it is odd or not.

Источник

Оцените статью