Python Program to Check Whether a Number is Positive or Negative or zero
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
def check(n):
if n > 0 :
print ( "Positive" )
elif n < 0 :
print ( "Negative" )
else :
print ( "Equal to zero" )
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)
|
Last Updated :
31 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...