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.
Output : The number is Positive.
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,
- Method 1: Using Brute Force
- Method 2: Using Nested if-else Statements
- 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
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
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
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
- ASCII Table
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Finding Prime Factors of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
Login/Signup to comment
Q. Positive or negative
n= int(input(“enter number”))
if(n<0):
print("negative number")
else:
print("positive number")
https://prepinsta.com/discord/
Kindly refer this link
# Positive or Negative number :
number = int(input(“Enter the number: “))
if number 0:
print(f”The {number} is positive”)
else:
print(“number is neither positive or negative”)
# Positive or Negative number :
number = int(input(“Enter the number: “))
if number 0:
print(f”The {number} is positive”)
else:
print(“number is neither positive or negative”)
I got plaed with 5 offers. All thanks to Prepnsta prime subscription. Want to thank you so much guys !
Python Program:
n=int(input(“Enter a number: “))
if n==0:
print(“The number is Zero”)
elif n>0:
print(“The number is Positive”)
else:
print(“The number is Negative”
if n==0:
print (“zero”)
elif n%2==0:
print (“pos”)
else:
print (“neg”)
Gud
num=int(input(“enter a number”))
if num==0:
print(“The number is zero”)
elif num>0:
print(“The number is positive”)
else:
print(“The number is negative”)
num = int(input(“Enter the number:”))
if num > 0:
print(f”This number is positive number: {num}.”)
elif num <0:
print(f"This is negative number: {num}.")
else:
print("Number is zero.")