Python Program to Check Even or Odd Number
Given a number and our task is to check number is Even or Odd using Python and If the number is even or odd then return true or false.
Example:
Input: 2 Output: Even number Input: 41 Output: Odd Number
Using Modulus operator check if number is even or odd
For that, we use the Operator Called Modulus Operator. This operator used in the operation when we need to calculate the remainder of that number when divided by any divisor.
Python3
x = 2 # x= 41 if x % 2 = = 0 : print (x, "Is Even Number" ) else : print (x, "Is Odd Number" ) |
Output:
2 Is Even Number
Time Complexity : O(1)
Auxiliary Space : O(1)
Using Ternary operator check if number is even or odd
Here we will use the ternary operator to check the number is divisible by 2 or not.
Python3
num = 5 print (num , "is Even" ) if num % 2 = = 0 else print (num, "is Odd" ) |
Output:
5 is Odd
Using recursion check if number is even or odd
Example 1:
We use the concept of getting the remainder without using the modulus operator by subtracting the number by number-2. If at last, we get any remainder then that number is odd and return the False for that number. Else the number is even and return True for that number
Python3
# defining the function having the one parameter as input def evenOdd(n): #if remainder is 0 then num is even if (n = = 0 ): return True #if remainder is 1 then num is odd elif (n = = 1 ): return False else : return evenOdd(n - 2 ) # Input by geeks num = 3 if (evenOdd(num)): print (num, "num is even" ) else : print (num, "num is odd" ) |
3 num is odd
Time Complexity : O(1)
Auxiliary Space : O(1)
Example 2:
We use the modulus operator to find the even or odd Like if num % 2 == 0 then the remainder is 0 so the given number is even and returns True, Else if num % 2 != 0 then the remainder is not zero so the given number is odd and return False
Python3
# defining the function having # the one parameter as input def evenOdd(n): #if remainder is 0 then num is even if (n % 2 = = 0 ): return True #if remainder is 1 then num is odd elif (n % 2 ! = 0 ): return False else : return evenOdd(n) # Input by geeks num = 3 if (evenOdd( num )): print (num , "num is even" ) else : print (num , "num is odd" ) |
3 num is odd
Time Complexity : O(1)
Auxiliary Space : O(1)
Using the AND (&) Operator To Check whether Number is Even or Odd
For this method, we just need to use the & operator, which is a bitwise operator, and it works the same way as the % operator for this particular case.
If given number & 1 gives 1, the number would be odd, and even otherwise.
Python3
# defining the function having # the one parameter as input def evenOdd(n): # if n&1 == 0, then num is even if n & 1 : return False # if n&1 == 1, then num is odd else : return True # Input by geeks num = 3 if (evenOdd(num)): print (num, "num is even" ) else : print (num, "num is odd" ) |
Output:
3 num is odd
Time Complexity : O(1)
Auxiliary Space : O(1)
Print all the even numbers for the range (a, b) using Recursion
Here We will print all the even numbers for the Given Range n in the Function evenOdd(n)
Python3
# writing the function having the range # and printing the even in that range def evenOdd(a,b): if (a>b): return print (a ,end = " " ) return evenOdd(a + 2 ,b) # input by geeks x = 2 y = 10 if (x % 2 = = 0 ): x = x else : # if the number x is odd then # add 1 the number into it to # avoid any error to make it even x + = 1 evenOdd(x,y) |
Output:
2 4 6 8 10
Time Complexity : O(1)
Auxiliary Space : O(1)
Print all even or odd numbers in a given range (a,b) without using Recursion
In this approach, we will see how we can find even numbers in a given range and print them without Recursion.
- First we will define a function that takes an integer input and checks if the number is even or odd. It returns 1 if the number is even, otherwise 0.
- We will define a starting value and an ending value, this will be our range in which we will find all the even numbers.
- We will run a loop from the starting value till the ending value (both inclusive) and check using our function that if that number is even or odd.
- We will print the value if the number is found even , otherwise, we will pass and go to the next number.
Python3
# Python program to find even # or odd in a given range # without using Recursion # defining the function # to check if the number # is even or odd def even_or_odd(num : int ) - > int : if num % 2 = = 0 : return 1 return 0 # Starting point of the range start = 2 # ending point of the range end = 10 # Iterating over each # element in the range and checking # if it is even or odd # using our function and printing for i in range (start,end + 1 ): if even_or_odd(i): print (i,end = " " ) else : pass |
Output:
2 4 6 8 10
Time Complexity – O(n) # n = the range in which we are searching.
Space Complexity – O(1) # Only two extra variables are being used.
Please Login to comment...