Python program to check a number is even or odd using function
- Home
- Check value
- Python program to check a number is even or odd using function
- On
- By
- 3 Comments
- Categories: Check value, Find elements
Python program to check a number is even or odd using function
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
3 Comments
sravan May 21, 2019 at 9:33 am
Write a function named collatz() that has one parameter named number. If number is even, then collatz() should print number // 2 and return this value. If number is odd, then collatz() should print and return 3 * number + 1.
Then write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1. (Amazingly enough, this sequence actually works for any integer—sooner or later, using this sequence, you’ll arrive at 1! Even mathematicians aren’t sure why. Your program is exploring what’s called the Collatz sequence, sometimes called “the simplest impossible math problem.”)
Remember to convert the return value from input() to an integer with the int() function; otherwise, it will be a string value.
Hint: An integer number is even if number % 2 == 0, and it’s odd if number % 2 == 1.
The output of this program could look something like this:
Enter number:
3
10
5
16
8
4
2
karan July 1, 2020 at 6:30 am
print(‘Enter a number = ‘)
x=input()
print(‘entered number is=’,x)
n = x % 2
k = 0
if n > k :
print(‘ Even ‘)
else :
print(‘Odd’)
ROHIT PRABHAKAR THAWALI November 12, 2021 at 4:52 am
num = int(input(‘enter any number’))
if num % 2:
print(‘number is odd’)
else:
print(‘number is even’)
IM A FUK BOY SHAFEEN December 15, 2022 at 3:16 pm
i hate this but this is like this
FUK U MY EMAIL [email protected]
i hate this dude shafeen mf raided my discord server fuk yalll i have grabbed his email with my token from the code i made from python 🙁