
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check whether product of integers from a to b is positive, negative or zero in Python
Suppose we have lower limit and upper limit of a range [l, u]. We have to check whether the product of the numbers in that range is positive or negative or zero.
So, if the input is like l = -8 u = -2, then the output will be Negative, as the values in that range is [-8, -7, -6, -5, -4, -3, -2], then the product is -40320 so this is negative.
To solve this, we will follow these steps −
- if l and u both are positive, then
- return "Positive"
- otherwise when l is negative and u is positive, then
- return "Zero"
- otherwise,
- n := |l - u| + 1
- if n is even, then
- return "Positive"
- return "Negative"
Let us see the following implementation to get better understanding −
Example Code
def solve(l,u): if l > 0 and u > 0: return "Positive" elif l <= 0 and u >= 0: return "Zero" else: n = abs(l - u) + 1 if n % 2 == 0: return "Positive" return "Negative" l = -8 u = -2 print(solve(l,u))
Input
-8, -2
Output
Negative
- Related Articles
- C program to Check Whether a Number is Positive or Negative or Zero?
- How to check if a number is positive, negative or zero using Python?
- Java Program to Check Whether a Number is Positive or Negative
- Haskell Program to Check Whether a Number is Positive or Negative
- C++ Program to Check Whether a Number is Positive or Negative
- How to check whether a number is Positive or Negative in Golang?
- Check if a number is positive, negative or zero using bit operators in C++
- What is the product of 6 negative and 12 positive integers?
- Python Program to Check if a Number is Positive, Negative or 0
- Program to check if a number is Positive, Negative, Odd, Even, Zero?
- C# Program to check if a number is Positive, Negative, Odd, Even, Zero
- What is zero if it is not a negative or positive number?
- How can we subtract two positive or negative integers?
- Check whether product of 'n' numbers is even or odd in Python
- Is it true that displacement of a body can be positive or zero, but never negative?

Advertisements