Python Program to check if a Number Is Positive Or Negative

Check if a Number is Positive and Negative in Python

Given an integer input, The objective is to write a code to Check if a Number is Positive or Negative in C Language.

Check if a Number is Positive or Negative in Python

Check if a Number is Positive and Negative in Python

Given an integer input, the objective is check whether the given integer is Positive or Negative. In order to do so we have the following method,

  1.  Method 1: Using Brute Force
  2. Method 2: Using Nested if-else Statements
  3. Method 3: Using ternary operator

We’ll discuss each method in-depth in the section below.

Method 1: Using Brute Force

This method uses Brute Force to check whether a given integer is Positive or Negative.

Python Code

Run
num = 15
if num > 0:
    print('Positive')
elif num < 0:
    print('Negative')
else:
    print('Zero')

Output

Positive

Algorithm

This method uses Brute Force to check whether a given integer is Positive or Negative.

Algorithm for the above code is as follows,

  • Initialize num as 15
  • If the num > 0: it is a positive number.
  • If the num < 0: it is a Negative number.
  • Else the number has to be zero itself

Method 2: Using Nested if-else Statements

This method uses a nested if-else Statements to check whether a given number is Positive or Negative.

Python Code

Run
num = 15
if num>=0:
    if num==0:
        print('Zero')
    else:
        print("Positive")
else:
    print("Negative")

Output

Positive

Algorithm

This method uses a nested if-else Statements to check whether a given number is Positive or Negative.

Algorithm for the above code is as follows,

  • Initialize num as 15
  • If the num >= 0
    • If num == 0 : num is zero
    • Else number has to be positive 
  • Else the number has to be negative

Method 3: Using Ternary Operator

This method uses a ternary operator to check whether a number is Positive or Negative.

Python Code

Run
num =15
print("Positive" if num>=0 else "Negative")

Output

Positive

Algorithm

This method uses a ternary operator in Python to check whether a number is Positive or Negative.

For a user input num

  • Return “Positive” if num>=0 else “Negative”.

Algorithm for the above code is as follows,

  • Initialize num as 15.
  • print the output using ternary operator in python using print () function.

Getting Started

31 comments on “Python Program to check if a Number Is Positive Or Negative”


  • kushalgr11

    positive or negative
    num =int(input(“enter the num “))
    if num>0:
    print (“you number is positive”)
    elif num<0:
    print("your number is negative ")
    else:
    print("your number is zero")