Python Program to check if a Number Is Positive Or Negative
Check if a Number is Positive or Negative in python
Given an integer input, the objective is to write a code to Check if a Number is Positive or Negative in python Language.
For Instance,
Input: Num = -5
Output: The number is Negative
Check if a Number is Positive or Negative in python
Given an integer input, the objective is to check whether the given integer is Positive or Negative. In order to do so we have the following methods:
Method 1: Using Brute Force
Method 2: Using Nested if-else Statements
Method 3: Using the ternary operato
- Method 1: Using Brute Force
num = int(input("Enter a number: ")) if num > 0: print("The number is Positive") elif num < 0: print("The number is Negative") else: print("The number is Zero")
Output:
Enter a number: -7 The number is Negative
- Method 2: Using Nested if-else Statements
Algorithm 1
For a user input num
If the num > 0: it is a positive number.
If the num < 0: it is a positive number.
Else the number has to be zero itself
Same logic we have followed in the below Python program.
Algorithm 2
1. Take input from the user.
2. If the number is greater than zero, print "Positive".
3. Else, check if the number is less than zero.
4. If true, print "Negative".
5. Otherwise, print "Zero".
num = int(input("Enter a number: ")) if num >= 0: if num == 0: print("The number is Zero") else: print("The number is Positive") else: print("The number is Negative")
Output:
Enter a number: 0 The number is Zero
Algorithm 3
1. Take input from the user.
2. Use the ternary operator to check if the number is positive, negative, or zero.
3. Print the result accordingly.
num = int(input("Enter a number: ")) print("The number is Positive" if num > 0 else "The number is Negative" if num < 0 else "The number is Zero")
Output:
Enter a number: 9 The number is Positive