Python program to check a number is even or odd using function
In this tutorial, we will discuss the Python program to check a number is even or odd using the function
In this program, we are going to learn about how to find the odd or even number from given number using function in the Python language.
First, we must understand what is even or odd?
What is Even or Odd
When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10
and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers or uneven numbers
In my previous post, I have explained the various ways to check whether the number is even or odd in Python language
However, in this program, we embed the logic of the check even or odd numbers program in the function
You can embed the logic of the program to check even or odd numbers using any of the following approaches in the function
Once This program received the number it will check the given number either odd or even.
After receiving the input from the user, it is stored in the variable of num and then program divides the value of num by 2 and displays the output
Using modular operator
num=int(input("Enter a number for check odd or even: ")) def find_Evenodd(num): if(num%2==0): print(num," Is an even") else: print(num," is an odd") find_Evenodd(num);//function call
When the above code is executed, it produces the following results
Case 1
Enter a number for check odd or even: 349 349 is an odd
Case 2
Enter a number for check odd or even: 234 234 is an even
Using Bitwise operator
num=int(input("Enter a number for check odd or even: ")) def find_Evenodd(num)://function definition if(num&1==1): print(num, "is an odd number"); else: print(num, "is an even number"); find_Evenodd(num);//function call
When the above code is executed, it produces the following results
Case 1
Enter a number for check odd or even: 678 678 is an even number
Case 2
Enter a number for check odd or even: 765 765 is an odd number
Using the division operator
num=int(input("Enter a number for check odd or even: ")) def find_Evenodd(num)://function definition number=(num/2)*2 if(number==num): print(num, "is a even number"); else: print(num, "is a odd number"); find_Evenodd(num);//call the function
When the above code is executed, it produces the following results
Case 1
Enter a number for check odd or even: 678 678 is a even number
Case 2
Enter a number for check odd or even: 987 987 is a odd number
Suggested for you
Similar post
Python program to check whether a number odd or even
Python program to display even and odd number in the given range
Python code to display all even and odd numbers from 1 to n
Python code to display all even and odd numbers without if