Write A Simple Python Function To Check Whether X Is Even Or Odd

In this article  let's learn about a Python program that uses a Python function to check if the given input number is even or.

let's define a function to check if the  give an integer is Even and Odd return the string containing the result Calling function

 

 

Write A Simple Python Function To Check Whether X Is Even Or Odd

 


Write A Simple Python Function To Check Whether X Is Even Or Odd

Define a simple Python function check_even_odd() by passing an integer as an input argument  to the function.  In the function Using the if statement try to divide the  input number by 2 and check if the remainder is 0 use the modulus operator to get the reminder.

If the remainder is zero then print the number is an even number else print the number is odd number

def check_even_odd(n):
   if n % 2 == 0:
       print(f"Number {n} is Even")
   else:
       print(f"Number {n} is Odd")
      

 in the main program let's take a variable named as number and ask a user to enter a number using input function call and then convert the input number into an integer using the type conversion.

Once the user enters a number Call the function check_even_odd() by passing the user input number.

number = int(input("Enter a Number : "))
check_even_odd(number)




Complete program
=======================
def check_even_odd(n):
   if n % 2 == 0:
       print(f"Number {n} is Even")
   else:
       print(f"Number {n} is Odd")
      

number = int(input("Enter a Number : "))
check_even_odd(number)




Output
===============
Enter a Number : 34
Number 34 is Even




Output
================
Enter a Number : 49
Number 49 is Odd



Output
===============
Enter a Number : 0
Number 0 is Even




Conclusion
====================
Execute the above program by yourself and  by providing the random integer as input to the Python program and note down the result,

Comment down below if you have any suggestions to improve the above program

Post a Comment

0 Comments