In this article, We will see how to write the program to find the given number is Even or Odd Using the Recursion Method. If the number is even returned true else false in Python.
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.
- Even Number: A number Which is completely divisible by 2 Hence remainders is 0
- Odd Number: A number which is not divisible by 2 Hence remainders is 1
Input: 2 Output: True Explanation : 2 % 2==0 So True Input: 5 Output: False Expalantion : 2 % 2 != 0 So, False
- 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
3 num is odd
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 return True
Else if num % 2 != 0 then the remainder is not zero so the given number is odd and return False
3 num is odd
Methods #3: To print all the even number for the range (a, b)
Here We will print all the even number for the Given Range n in the Function evenOdd(n)
2 4 6 8 10
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course