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: A number that is divisible by 2.
  • Odd: A number that is not divisible by 2.

Let's explore different approaches to implement these classifications in Python.

Using if-else Statements

The Python if-else statements are conditional statements that execute different code blocks based on conditions. We can chain multiple conditions to check all number properties ?

Syntax

if condition:
    # code block
else:
    # code block

Example 1: Checking Positive Number

Let's check the number 4 and determine its properties ?

def check_number(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.")

check_number(4)
It is Positive.
It is Even.

Example 2: Checking Negative Number

Now let's test with a negative number -3 ?

def check_number(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.")

check_number(-3)
It is Negative.
It is Odd.

Example 3: Checking Zero

Let's also test with zero to see how it behaves ?

def check_number(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.")

check_number(0)
It is Zero.

Using Modulus (%) Operator

The Python modulus operator (%) returns the remainder after division. For even/odd checking, we use num % 2 ? if the result is 0, the number is even; otherwise, it's odd.

remainder = number % 2

Enhanced Function with All Cases

Here's a comprehensive function that handles all number classifications ?

def classify_number(num):
    classifications = []
    
    # Check zero first
    if num == 0:
        classifications.append("Zero")
        classifications.append("Even")  # Zero is considered even
    else:
        # Check positive/negative
        if num > 0:
            classifications.append("Positive")
        else:
            classifications.append("Negative")
        
        # Check even/odd
        if num % 2 == 0:
            classifications.append("Even")
        else:
            classifications.append("Odd")
    
    return classifications

# Test with different numbers
test_numbers = [4, -3, 0, 7, -8]

for number in test_numbers:
    result = classify_number(number)
    print(f"{number}: {', '.join(result)}")
4: Positive, Even
-3: Negative, Odd
0: Zero, Even
7: Positive, Odd
-8: Negative, Even

Summary Table

Number Sign Parity Special Case
4 Positive Even None
-3 Negative Odd None
0 Neither Even Zero

Conclusion

Use conditional statements with >, , and == operators to check if a number is positive, negative, or zero. The modulus operator % effectively determines if a number is even or odd by checking the remainder when divided by 2.

Updated on: 2026-03-24T21:02:43+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements