Table of contents

Python Program to check whether a given integer is positive, negative or zero

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

  1. Start
  2. Take input: Prompt the user to enter a number.
  3. 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.
  4. Display the result: Print whether the number is positive, negative, or zero.
  5. 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

  1. Taking input from the user:
    The program uses the input() function to take input from the user. The input is converted to a float so that it can handle both integers and decimal numbers.
  2. Checking the number:
    The program uses an if-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.
  3. Displaying the output:
    The print() function is used to display whether the number is positive, negative, or zero based on the result of the conditionals.

Python

Related Articles