Open In App

Python Program to Check Whether a Number is Positive or Negative or zero

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number. The task is to check whether the number is positive or negative or zero.

Examples:

Input: 5
Output: Positive

Input: -5
Output: Negative

Approach:

We will use the if-elif statements in Python. We will check whether the number is greater than zero or smaller than zero or equal to zero.

Below is the implementation.

Python3




# Python program to check whether
# the number is positive, negative
# or equal to zero
 
def check(n):
     
    # if the number is positive
    if n > 0:
        print("Positive")
         
    # if the number is negative
    elif n < 0:
        print("Negative")
         
    # if the number is equal to
    # zero
    else:
        print("Equal to zero")
         
# Driver Code
check(5)
check(0)
check(-5)


Output:

Positive
Equal to zero
Negative

The time complexity of this program is O(1). This is because all of the operations are performed in constant time, regardless of the size of the input. The program only performs three operations, so its time complexity remains constant regardless of the size of the input.

The space complexity of this program is also O(1). This is because the only additional space used is to store the input number, which is a constant size regardless of the size of the input.

Method: Using the list comprehension method 

Python3




n=5
x=["positive" if n>0 else "negative" if n<0 else "zero" ]
print(x)


Output

['positive']


Last Updated : 31 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads