
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check if a number is Positive, Negative, Odd, Even, Zero?
In this article, we are going to learn how to check if the number is positive, negative, odd, even, or zero. Identifying and categorizing a number based on its characteristics is a basic operation. A number can fall into multiple categories:
- Positive: A number is greater than 0.
- Negative: A number less than 0.
- Zero: The number that is equal to 0.
- Even: The number that is divisible by 2.
- Odd: The number that is not divisible by 2.
Let's dive into the article to get more ideas about these classifications.
Using if, else Statements
The Python if, else statements are the conditional statements that are used to execute a block of code when the condition in the if statement is true, and another block of code when the condition is false.
Syntax
Following is the syntax of the Python if-else statement -
if: ....... else: ......
Using Modulus (%) Operator
The Python modulus operator is also called the remainder operator because it is used to return the remainder of the division of two numeric operands. Following is the syntax of the Python modulus operator -
x = a % b
Example 1
Let's look at the following example, where we are going to consider the number 4 and check whether it is positive, negative, odd, even, or zero.
def demo(num): if num == 0: print("It is Zero.") else: if num > 0: print("It is Positive.") else: print("It is Negative.") if num % 2 == 0: print("It is Even.") else: print("It is Odd.") demo(4)
The output of the above program is as follows -
It is Positive. It is Even.
Example 2
Consider another scenario, where we are going to consider the number -3 and check whether it is positive, negative, odd, even, or zero.
def demo(num): if num == 0: print("It is Zero.") else: if num > 0: print("It is Positive.") else: print("It is Negative.") if num % 2 == 0: print("It is Even.") else: print("It is Odd.") demo(-3)
The output of the above program is as follows -
It is Negative. It is Odd.