Hello everyone, welcome to Programming In Python. In this post, I will develop a simple logic to check whether an integer is an even or odd number in Python. Any number(positive/negative) which is divisible by the number 2 is declared as an even number and numbers( positive/negative ) which are not divisible by the number 2 are declared as odd numbers.
Even or Odd – Code Visualization
To check whether an integer is an even number or an odd number.
- Read an input integer using
- When the input is divided by 2 and leaves a remainder 0, it is said to be EVEN number.
- When the input is divided by 2 and leaves any remainder other than 0, it is said to be ODD number.
- When an input is 0 it is said to be even.
- The same conditions apply to negative integers too.
Video Demonstration :
You can also watch the video on YouTube here.
input_num = int(input('Enter any number: ')) if input_num % 2 == 0: print(input_num, "is EVEN") else: print(input_num, "is ODD")
- Test Case 1: Positive even number
- Test Case 2: Positive odd number
- Test Case 3: Negative even number
- Test Case 4: Negative odd number
- Test Case 5: Zero
Want to be strong at OOP in Python? If yes, I would suggest you to take the course below.
Object Oriented Programming in Python