Python Program to Check if a Number is Positive or Negative

1. Introduction

Determining whether a number is positive or negative is a basic yet essential task in programming, often serving as an introduction to conditional statements. This operation is fundamental in a wide range of applications, from financial software that needs to differentiate between credits and debits, to scientific calculations where the sign of a number can significantly alter the outcome. This blog post will explore a Python program designed to check if a given number is positive or negative, highlighting the simplicity and effectiveness of using conditional statements in Python.

2. Program Steps

1. Prompt the user to enter a number.

2. Check if the number is positive, negative, or zero using conditional statements.

3. Display the result based on the condition.

3. Code Program

# Step 1: Prompt the user to enter a number
num = float(input("Enter a number: "))

# Step 2: Use conditional statements to determine if the number is positive, negative, or zero
if num > 0:
    print("The number is positive.")
elif num == 0:
    print("The number is zero.")
else:
    print("The number is negative.")

Output:

Enter a number: -5
The number is negative.

Explanation:

1. The program begins by asking the user to input a number. This number is stored as a floating-point value to accommodate decimal numbers, enhancing the program's flexibility.

2. It then evaluates the input number using a series of if-elif-else statements. The first if checks if the number is greater than zero, in which case it's positive. The elif checks if the number is exactly zero. If neither of these conditions is true, the else block executes, indicating that the number is negative.

3. The outcome of the evaluation is printed directly within the conditional statements, informing the user if the entered number is positive, negative, or zero. This example demonstrates the use of conditional statements to make simple decisions based on numerical input, a fundamental skill in programming.


Comments