If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In Python, you can determine whether a given number is even or odd using conditional statements and the modulo operator (%). The modulo operator returns the remainder when a number is divided by another number. Here's how you can check if a number is even or odd:
pythonCopy code
def check_even_odd(number): if number % 2 == 0: print(f"{number} is even.") else: print(f"{number} is odd.") # Example usage: num = 7 check_even_odd(num) # Output: 7 is odd. num = 12 check_even_odd(num) # Output: 12 is even.
In this example, the
check_even_odd()
function takes a number as input and uses the modulo operator%
to check if the number is divisible by 2 without any remainder. If the remainder is 0 (i.e., the number is even), the function prints "{number} is even." Otherwise, it prints "{number} is odd."You can call the
check_even_odd()
function with different numbers to determine if they are even or odd. The function will return the appropriate message for each case.
Comments: 0