Write a Python program that checks whether a given integer is positive, negative or zero.
Input-Output Examples
plaintext
Example 1:
Input: Enter a number: 5
Output: The number is positive.
Example 2:
Input: Enter a number: -3
Output: The number is negative.
Example 3:
Input: Enter a number: 0
Output: The number is zero.
Algorithm to check whether a given number is positive, negative, or zero
- Start
- Take input: Prompt the user to enter a number.
- Check the number:
- If the number is greater than 0, it is positive.
- If the number is less than 0, it is negative.
- If the number is equal to 0, it is zero.
- Display the result: Print whether the number is positive, negative, or zero.
- End
Python Program to check whether a given number is positive, negative, or zero
python
# Python program to check if a number is positive, negative, or zero
# Take input from the user
num = float(input("Enter a number: "))
# Check if the number is positive, negative, or zero
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
Code Explanation
- Taking input from the user:
The program uses theinput()
function to take input from the user. The input is converted to afloat
so that it can handle both integers and decimal numbers. - Checking the number:
The program uses anif-elif-else
structure to determine whether the number is positive, negative, or zero:- If
num > 0
, the number is positive. - If
num < 0
, the number is negative. - If
num == 0
, the number is zero.
- If
- Displaying the output:
Theprint()
function is used to display whether the number is positive, negative, or zero based on the result of the conditionals.