To Check Whether a Number is Positive or Negative in python

The provided Python program is designed to determine whether a given number is positive, negative, or zero. It begins by prompting the user to enter a numerical value using the input() function. The input is then converted to a floating-point number using the float() function to handle decimal values. The program proceeds to evaluate the number using an if-elif-else statement. If the number is greater than zero, it prints “The number is positive.” If the number is less than zero, it prints “The number is negative.” Finally, if the number is exactly zero, it prints “The number is zero.” This straightforward program allows users to quickly ascertain the sign of a given numerical input.


Source code

# Get 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.")

Description

  1. User Input:
    • The program begins by using the input() function to prompt the user to enter a number.
    • The entered value is then stored in the variable num.

num = float(input("Enter a number: "))

  1. Data Conversion:
    • The entered value is converted to a floating-point number using the float() function. This step is crucial for handling decimal values.

num = float(input("Enter a number: "))

  1. Conditional Checking:
    • The program uses an if-elif-else statement to check the value of the entered number.
    • If the number is greater than zero, it prints “The number is positive.”
    • If the number is less than zero, it prints “The number is negative.”
    • If the number is exactly zero, it prints “The number is zero.”

if num > 0: print("The number is positive.") elif num < 0: print("The number is negative.") else: print("The number is zero.")

  1. Output:
    • The program prints the result based on the conditions met. If the user enters 5, it will print “The number is positive.” If the user enters -3, it will print “The number is negative.” If the user enters 0, it will print “The number is zero.”

This program efficiently determines the sign of the entered number and provides a clear output based on the specified conditions.


Screenshot


Download

By Admin

Leave a Reply

Your email address will not be published. Required fields are marked *