Even odd program in python using if else
A number is even if it is perfectly divisible by 2. When the number is divided by 2, we use the remainder operator Show
Source Code
Output 1 Enter a number: 43 43 is Odd Output 2 Enter a number: 18 18 is Even In this program, we ask the user for the input and check if the number is odd or even. Please note that Python Program to Check if a Number is Odd or EvenOdd and Even numbers: If you divide a number by 2 and it gives a remainder of 0 then it is known as even number, otherwise an odd number. Even number examples: 2, 4, 6, 8, 10, etc. Odd number examples:1, 3, 5, 7, 9 etc. See this example: Output: For Videos Join Our Youtube Channel: Join Now Feedback
Help Others, Please SharePython Basic: Exercise-21 with SolutionWrite a Python program to find whether a given number (accept from the user) is even or odd, print out an appropriate message to the user. Pictorial Presentation of Even Numbers: Pictorial Presentation of Odd Numbers: Sample Solution:- Python Code:
Sample Output: Enter a number: 5 This is an odd number. Even Numbers between 1 to 100: Odd Numbers between 1 to 100: Flowchart: Visualize Python code execution:The following tool visualize what the computer is doing step-by-step as it executes the said program: Python Code Editor: Have another way to solve this solution? Contribute your code (and comments) through Disqus. Previous: Write a Python program to get a string which is n (non-negative integer) copies of a given string. Check Whether a Number is Even or Odd in PythonGiven an integer input num, the objective is to write a code to Check Whether a Number is Even or Odd in Python. To do so we check if the number is divisible by 2 or not, it’s Even if it’s divisible otherwise Odd. Example
Input : num = 3
Output : Odd Check Whether a Number is Even or Odd in PythonGiven an integer input the objective is to write a Python code to Check Whether a Number is Even or Odd. To do so the main idea is to divide the number by 2 and check if it’s divisible or not. It’s an Even number is it’s perfectly divisible by 2 or an Odd number otherwise. Here are the Methods to solve the above mentioned problem,
We’ll discuss the above mentioned methods in detail in the next section.
Method 1 : Using Brute ForceThis method simply checks if the given input integer is divisible by 2 or not. If it’s divisible then print Even or Odd otherwise. Python CodeRun num = int(input("Enter a Number:")) if num % 2 == 0: print("Given number is Even") else: print("Given number is Odd") OutputEnter a Number: 5 Given number is Odd WorkingThe working of the above mentioned code is as follows,
ExplanationGiven an integer as input, the objective is check whether the number is even or odd in Python. To do so we check if it’s divisible by 2 or not. If true, it’s even or it’s odd otherwise. The algorithm for the above code is as given below,
The output for the above code is wither even or odd based on whether or not it’s divisible by 2. Method 2 : Using Ternary OperatorThis Method uses the ternary operator to check if the integer input is divisible by 2, If true print Even or Odd otherwise. Ternary Operator Syntax Python ( True : Action ) if ( Condition ) else ( False : Action ) Python CodeRun num = 17 print("Even") if num%2 == 0 else print("Odd") OutputWorkingThe working of the above code is as follows,
ExplanationGiven an integer as input, the objective is check whether the number is even or odd in Python. To do so we check if it’s divisible by 2 or not using a Ternary Operator in Python. If true, it’s even or it’s odd otherwise. The algorithm for the above code is as given below,
The output for the above code is wither even or odd based on whether or not it’s divisible by 2.
Method 3 : Using Bitwise OperatorThis Method uses bitwise operators to check if a given number is Even or Odd. Bitwise Operators In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operations and directly supported by the processor. Python CodeRun def isEven(num): return not num&1 if __name__ == "__main__": num = 13 if isEven(num): print('Even') else: print('Odd') OutputWorkingThe working of the above code is as follows,
ExplanationGiven an integer as input, the objective is check whether the number is even or odd in Python. To do so we check if it’s divisible by 2 or not using Bitwise Operator. If true, it’s even or it’s odd otherwise. The algorithm for the above code is as given below,
The output for the above code is wither even or odd based on whether or not it’s divisible by 2. Prime Course Trailer
Related BannersGet PrepInsta Prime & get Access to all 150+ courses offered by PrepInsta in One Subscription
Get over 150+ course One SubscriptionCourses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Checkout list of all the video courses in PrepInsta Prime SubscriptionCheckout list of all the video courses in PrepInsta Prime SubscriptionGetting Started
How do you find the odd number in python without if else?Python Code:
num = int(input("Enter a number: ")) mod = num % 2 if mod > 0: print("This is an odd number. ") else: print("This is an even number. ")
How do you separate even and odd in Python?Step 1 : create a user input list. Step 2 : take two empty list one for odd and another for even. Step 3 : then traverse each element in the main list. Step 4 : every element is divided by 2, if remainder is 0 then it's even number and add to the even list, otherwise its odd number and add to the odd list.
|